文章目录
BitmapDrawable 例子
- antialias:是否开启图片抗锯齿功能。
- dither:是否开启抖动效果。
- gravity:可以配置图片的显示位置,如果控件的宽高大于图片的话,可以设置具体显示的位置
- tileMode:平铺模式。“disabled” “clamp” “repeat” “mirror”
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_launcher"
android:antialias="true"
android:dither="true"
android:tileMode="repeat"
>
</bitmap>
ShapeDrawable 例子
描边(实线)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#FEE4B1" />
<corners android:radius="4dp" />
</shape>
描边(虚线)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="3dp"
android:color="#ff0000"
android:dashWidth="2dp"
android:dashGap="3dp" />
<corners android:radius="5dp"/>
</shape>
渐变色(左右)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<!-- gradient节点中angle的值270是从上到下,0是从左到右,90是从下到上 -->
<gradient
android:angle="0"
android:endColor="#FCECD6"
android:startColor="#FFD595"
android:type="linear" />
</shape>
设置shape大小
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="6dp" />
<solid android:color="#00ff00" />
<size android:height="2dp" android:width="50dp"/>
</shape>
垂直虚线
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#EBB06D"
android:dashWidth="3dp"
android:dashGap="3dp"
/>
</shape>
</rotate>
LayerDrawable 例子
自定义proressBar样式
home_smart_sdcard_progress 文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="#BBD4FB" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="5dip" />
<solid android:color="#397BFF" />
</shape>
</scale>
</item>
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="5dip" />
<solid android:color="#80397BFF" />
</shape>
</scale>
</item>
</layer-list>
progressBar 的 Style
<style name="ProgressBar_Scale" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/home_smart_sdcard_progress</item>
</style>
progressBar 使用
<ProgressBar
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_container"
android:id="@+id/progressBar"
style="@style/ProgressBar_Scale"
android:layout_width="150dp"
android:layout_height="6dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:progress="50"/>
StateListDrawable 例子
图片
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/v4_op_cancel_full_pre" android:state_pressed="true" />
<item android:drawable="@drawable/v4_op_cancel_full_dis" android:state_enabled="false" />
<item android:drawable="@drawable/v4_op_cancel_full_nor" />
</selector>
帧动画
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/v3_vip_loading_1"
android:duration="50" />
<item
android:drawable="@drawable/v3_vip_loading_2"
android:duration="50" />
</animation-list>
使用
imgShow.drawable?.let {
if(it is AnimationDrawable){
it.start()
}
}