APP开发实战146-减少预置图片的方案
2016-12-15 22:44
405 查看
1 APP有时会使用到上下左右箭头这类内容一样,方向不同的图片,可以只预置一张上的箭头图标,下左右箭头可以使用代码旋转上箭头图标实现;这样只需预置一张图片,也减少了APP的大小。
在Google的官方文档中,有如下说明:
You can also omitresources that are only a rotated equivalent of another resource. The followingcode snippet provides an example of turning an "expand" arrow into a"collapse" arrow icon by simply rotating the original image 180degrees:
(https://developer.android.com/topic/performance/reduce-apk-size.html)
如下图两个图标箭头都是朝右的:
对应的XML代码如下:
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageView" />
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageViewLeft"/>
如果想把第二个改成箭头向左的图标,可按如下方式实现:
//定义旋转功能的XML代码
<?xmlversion="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
//具体实现代码
Animation
rotateAnimation= AnimationUtils.loadAnimation(this, R.anim.rotate);
ImageView
imageViewLeft= (ImageView) findViewById(R.id.imageViewLeft);
imageViewLeft.startAnimation(rotateAnimation);
//图片旋转后,不恢复原状
rotateAnimation.setFillAfter(true);
结果如下,只用一张图,通过代码,实现了两种显示效果:
2 使用tint和tintmode属性减少预置的图片资源。当只是要改变图片内容的颜色,而不改变图片内容时,以往做法是预置几张不同颜色的图片,使用这两个属性,只需预置一张图片就可以了,程序运行时,动态改变图片的颜色。
在Google的官方文档中,有如下说明:
You can include a separate resourcefor variations of an image, such as tinted, shaded, or rotated versions of thesame image. We recommend, however, that you reuse the same set of resources,customizing them as needed at runtime.
Android provides several utilities tochange the color of an asset, either usingthe android:tint and tintMode attributes on Android 5.0(API level 21) and higher. For lower versions of the platform, use the ColorFilter class.
(https://developer.android.com/topic/performance/reduce-apk-size.html)
如下界面:
对应的XML代码如下:
<ImageView
android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login"/>
可以使用tint属性设置图片里显示的内容不变,但颜色改变,如下所示:
<ImageView
android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login"
android:tint="#EEEEEE"/>
此时界面变成如下所示:
也可以通过代码动态设置,代码如下:
ImageViewimageView = (ImageView) findViewById(R.id.login_image);
imageView.setColorFilter(Color.GRAY);
在许多APP输入密码的编辑框右边都有一个图标,反复点击图标,图标会显示不同的颜色,同时密码会以明文或密文形式显示。传统方式也是预置两张不同颜色的图片,使用tint和tintmode属性只需预置一张图片就可以了,程序运行时,动态改变图片的颜色。
如下所示界面:
对应的XML代码如下:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/login_eye_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="12dp"
android:background="@drawable/login_eye_first"
android:gravity="center"/>
设置tint和tintmode属性:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/login_eye_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="12dp"
android:background="@drawable/login_eye_first"
android:gravity="center"
android:backgroundTint="@color/colorAccent"
android:backgroundTintMode="screen" />
界面如下:
也可以通过代码动态设置,代码如下:
privatevoid setDisplayPassword() {
mIsDisplayPassword =!mIsDisplayPassword;
if (mIsDisplayPassword){
ViewCompat.setBackgroundTintList(mLoginEyeEt,ColorStateList.valueOf(Color.parseColor("#FF4081")));
ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
mEditPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
ViewCompat.setBackgroundTintList(mLoginEyeEt,ColorStateList.valueOf(Color.parseColor("#CCCCCC")));
ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
mEditPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
使用AppCompatTextView控件是为了在低于Android 5.0 (APIlevel 21)的系统中也可以使用tint和tintmode属性。
在Google的官方文档中,有如下说明:
You can also omitresources that are only a rotated equivalent of another resource. The followingcode snippet provides an example of turning an "expand" arrow into a"collapse" arrow icon by simply rotating the original image 180degrees:
(https://developer.android.com/topic/performance/reduce-apk-size.html)
如下图两个图标箭头都是朝右的:
对应的XML代码如下:
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageView" />
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageViewLeft"/>
如果想把第二个改成箭头向左的图标,可按如下方式实现:
//定义旋转功能的XML代码
<?xmlversion="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
//具体实现代码
Animation
rotateAnimation= AnimationUtils.loadAnimation(this, R.anim.rotate);
ImageView
imageViewLeft= (ImageView) findViewById(R.id.imageViewLeft);
imageViewLeft.startAnimation(rotateAnimation);
//图片旋转后,不恢复原状
rotateAnimation.setFillAfter(true);
结果如下,只用一张图,通过代码,实现了两种显示效果:
2 使用tint和tintmode属性减少预置的图片资源。当只是要改变图片内容的颜色,而不改变图片内容时,以往做法是预置几张不同颜色的图片,使用这两个属性,只需预置一张图片就可以了,程序运行时,动态改变图片的颜色。
在Google的官方文档中,有如下说明:
You can include a separate resourcefor variations of an image, such as tinted, shaded, or rotated versions of thesame image. We recommend, however, that you reuse the same set of resources,customizing them as needed at runtime.
Android provides several utilities tochange the color of an asset, either usingthe android:tint and tintMode attributes on Android 5.0(API level 21) and higher. For lower versions of the platform, use the ColorFilter class.
(https://developer.android.com/topic/performance/reduce-apk-size.html)
如下界面:
对应的XML代码如下:
<ImageView
android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login"/>
可以使用tint属性设置图片里显示的内容不变,但颜色改变,如下所示:
<ImageView
android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login"
android:tint="#EEEEEE"/>
此时界面变成如下所示:
也可以通过代码动态设置,代码如下:
ImageViewimageView = (ImageView) findViewById(R.id.login_image);
imageView.setColorFilter(Color.GRAY);
在许多APP输入密码的编辑框右边都有一个图标,反复点击图标,图标会显示不同的颜色,同时密码会以明文或密文形式显示。传统方式也是预置两张不同颜色的图片,使用tint和tintmode属性只需预置一张图片就可以了,程序运行时,动态改变图片的颜色。
如下所示界面:
对应的XML代码如下:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/login_eye_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="12dp"
android:background="@drawable/login_eye_first"
android:gravity="center"/>
设置tint和tintmode属性:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/login_eye_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="12dp"
android:background="@drawable/login_eye_first"
android:gravity="center"
android:backgroundTint="@color/colorAccent"
android:backgroundTintMode="screen" />
界面如下:
也可以通过代码动态设置,代码如下:
privatevoid setDisplayPassword() {
mIsDisplayPassword =!mIsDisplayPassword;
if (mIsDisplayPassword){
ViewCompat.setBackgroundTintList(mLoginEyeEt,ColorStateList.valueOf(Color.parseColor("#FF4081")));
ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
mEditPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
ViewCompat.setBackgroundTintList(mLoginEyeEt,ColorStateList.valueOf(Color.parseColor("#CCCCCC")));
ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
mEditPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
使用AppCompatTextView控件是为了在低于Android 5.0 (APIlevel 21)的系统中也可以使用tint和tintmode属性。
相关文章推荐
- APP开发实战162-减少预置图片所占空间大小方法汇总
- APP开发实战160-WebP格式图片简介
- APP开发实战175-针对Android7.0及更高版本的后台优化方案
- APP开发实战161-图片格式的选择
- APP开发实战164-Evernote的JobScheduler方案
- APP开发实战10-APP数据同步方案
- APP开发实战165-利用工具减少APP大小
- APP开发实战9-API接口安全方案
- APP开发实战117-APP图片优化
- android开发图片分辨率问题解决方案
- C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。
- 混合式APP开发中中间件方案Rexsee
- 一款基于 Android 开发的离线版的 MM 图片浏览 App
- 【ios开发】iOS App测试方案
- ios开发者创建app应用开发授权文件 实战方法
- CSS3实战开发: 纯CSS实现图片过滤分类显示特效
- 《深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)》
- iOS应用程序开发——解决iOS7之前版本与之后版本下app启动图片跳动问题
- 移动应用开发(IOS/android等)中一个通用的图片缓存方案讲解(附流程图)
- android开发图片分辨率问题解决方案