您的位置:首页 > 移动开发

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属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: