一.ColorDrawable
颜色设置0xff0000会出问题,透明度不可省略,改为0xffff0000,则正常显示
ColorDrawable colorDrawable = new ColorDrawable(0xffff0000); imageView.setImageDrawable(colorDrawable);同时知道API小于16时,设置ImageView的背景要用setBackgroundDrawable方法
private void setBackgroundOfVersion(View view, Drawable drawable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { //Android系统大于等于API16,使用setBackground view.setBackground(drawable); } else { //Android系统小于API16,使用setBackground view.setBackgroundDrawable(drawable); } }ColorDrawable还可以用xml的方式进行创建:
在drawable文件夹下新建文件colorf.xml文件:
<?xml version="1.0" encoding="utf-8"?> <color xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorPrimary" />
二。使用shapeDrawable的gradient时,
要设置userLevel = false,否则看不到画面
android:innerRadius——指定圆环内圆的半径,比如50dp、100dp之类的。
android:innerRadiusRatio——该值是以比例的形式来指定内圆半径。内圆半径等于该shape的宽除以该值。或者说该值的倒数代表了内圆半径占整个shape宽的比例。默认值是9。当该值等于2的时候,内圆就将占满整个shape,从而我们将看不到圆环。
android:thickness——指定圆环的宽窄,也就是内圆与外圆的距离。
android:thicknessRatio——以比例的形式来指定圆环的宽窄。其算法与innerRadiusRatio相同。
渐变有三种:径向渐变,线性渐变,扫描渐变
径向渐变 需设置gradientRaius才能看到画面
<shape android:shape="ring" xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="1dp" android:thickness="20dp" android:useLevel="false"> <gradient android:type="radial" android:gradientRadius="20dp" android:startColor="#ffff0000" android:centerColor="#ff00ff00" android:endColor="#ff0000ff" /> </shape>线性:
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="ring" xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="10dp" android:thickness="40dp" android:useLevel="false"> <gradient android:type="linear" android:startColor="#ffff0000" android:centerColor="#ff00ff00" android:endColor="#ff0000ff" /> </shape>扫描:
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="ring" xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="10dp" android:thickness="40dp" android:useLevel="false"> <gradient android:type="sweep" android:startColor="#ffff0000" android:centerColor="#ff00ff00" android:endColor="#ff0000ff" /> </shape>三。bitmapDrawable
镜像效果:
<?xml version="1.0" encoding="utf-8"?> <bitmap android:src="@drawable/a" xmlns:android="http://schemas.android.com/apk/res/android" android:tileMode="mirror" />
像素扩展:
<?xml version="1.0" encoding="utf-8"?> <bitmap android:src="@drawable/a" xmlns:android="http://schemas.android.com/apk/res/android" android:tileMode="clamp" />
重复:
<?xml version="1.0" encoding="utf-8"?> <bitmap android:src="@drawable/a" xmlns:android="http://schemas.android.com/apk/res/android" android:tileMode="repeat" />
四。ClipDrawable
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@drawable/a" android:gravity="left"> </clip> 需在代码中对drawable 使用setLevel控制Level(0~10000),Level越多显示越多
五。scaleDrawable
对图像进行缩放
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/a" android:scaleHeight="50%" android:scaleWidth="50%" android:scaleGravity="center" /> 同样要使用setLevel控制Level七。rotateDrawable
对图像进行旋转
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/a" android:pivotX="50" android:pivotY="50" android:fromDegrees="10" android:toDegrees="360" />
还有 AnimationDrawable ,LayerDrawable ,LevelListDrawable, StateListDrawable,
以及控制从一层动透明度变化到另一层的TransitionDrawable 。
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/a"/> <item android:drawable="@drawable/colord" /> </transition>
drawable = (TransitionDrawable)getResources().getDrawable(R.drawable.colorf); new Thread(new Runnable() { @Override public void run() { drawable.startTransition(2000); } }).start();