需求:最新开发,有几个渐变色的背景,因为图片比较大,为减小应用体积,用drawable资源文件来加载,然后代码转为Bitmap在设置背景。
实现:
1、drawable文件(色值随意定的,只是demo):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--实现应用背景颜色渐变-->
<gradient
android:startColor="@color/colorAccent"
android:endColor="@color/colorPrimaryDark"
android:angle="270"/>
</shape>
2、代码中转Bitmap
private Bitmap getBitmapGradient(){
Drawable drawable = mContext.getResources().getDrawable(R.drawable.background_gradient);
GradientDrawable gradientDrawable = (Gradie

这篇博客介绍了如何在Android中将XML定义的渐变背景转换为Bitmap,以减小应用体积。通过创建GradientDrawable对象,设置宽高,然后在Canvas上绘制到Bitmap中,实现了从drawable资源到Bitmap的转换。
最低0.47元/天 解锁文章
7938

被折叠的 条评论
为什么被折叠?



