Android 中如何在代码中动态设置,桌面小部件【背景颜色、圆角、颜色、透明度】的两种实现方法
目录
shape配合代码实现桌面小部件【背景颜色、颜色、透明度】
这种方式是利用shape加代码实现一个背景图,这种方式唯一缺点就是不能动态设置矩形的圆角
1.在drawable目录下创建【widget_background.xml】
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="5dp" /> <!-- 特别注意此处,不要设置透明颜色,除非你的背景透明颜色是固定的 --> <solid android:color="#000000"/> </shape>
2.在小部件的布局中添加一个【ImageView】用来作为背景使用
注意 tools:background="@drawable/widget_background" 是在视图编辑界面预览用的,实际运行时不会有该效果
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_appwidget_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" tools:background="@drawable/widget_background" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="小部件文字内容" android:textSize="25dp" android:textColor="#FFFFFF"/> </FrameLayout>
3.最后就是在代码中动态设置小部件的背景透明度、颜色,这种方式不能动态设置圆角
// 设置之前创建的shape资源作为背景 views.setInt(R.id.iv_appwidget_bg, "setImageResource", R.drawable.widget_background); // 设置颜色用于着色,意思就是将widget_background的颜色着色成你指定的颜色 views.setInt(R.id.iv_appwidget_bg, "setColorFilter", Color.BLUE); // 设置透明度 0到255 views.setInt(R.id.iv_appwidget_bg, "setAlpha", 85);
纯代码实现桌面小部件【背景颜色、圆角、颜色、透明度】
1.在小部件的布局中添加一个【ImageView】用来作为背景使用,注意这种方式需要设置android:scaleType="fitXY"属性
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_appwidget_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:scaleType="fitXY" tools:background="@drawable/widget_background" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="小部件文字内容" android:textSize="25dp" android:textColor="#FFFFFF"/> </FrameLayout>
2.代码中实现不在细说附上背景绘制的工具类
/** * dengmengxin 码字不易搬运注明出处 */ public final class DrawableUtils { public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) { final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); DrawableCompat.setTintList(wrappedDrawable, colors); return wrappedDrawable; } public static Bitmap drawableToBitmap(Drawable drawable) { // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; } public static Drawable roundShapeDrawable(int color, @IntRange(from = 0, to = 100) int alpha, int width, int height, float radius) { ShapeDrawable shapeDrawable = new ShapeDrawable(); // 外部矩形的8个半圆角半径,一个圆角由2个半圆角组成 float[] externalRound = {radius, radius, radius, radius, radius, radius, radius, radius}; // 内部矩形与外部矩形的距离 RectF distanceRectF = null;//new RectF(10, 10, 10, 10); // 内部矩形的8个半圆角半径值 float[] insideRound = null;//{10, 10, 10, 10, 10, 10, 10, 10}; RoundRectShape roundRectShape = new RoundRectShape(externalRound, distanceRectF, insideRound); shapeDrawable.setShape(roundRectShape); shapeDrawable.setAlpha((int) (alpha * 2.55)); shapeDrawable.getPaint().setAntiAlias(true); shapeDrawable.getPaint().setDither(true); shapeDrawable.getPaint().setColor(color); shapeDrawable.setIntrinsicWidth(width); shapeDrawable.setIntrinsicHeight(height); return shapeDrawable; } }
3.最后就是在代码中动态设置小部件的背景颜色、圆角、颜色、透明度
// 背景颜色 int color = Color.BLUE; // 透明度 int alpha = 50; // 背景宽度 int width = context.getResources().getDimensionPixelSize(R.dimen.dp_250); // 背景高度 int height = context.getResources().getDimensionPixelSize(R.dimen.dp_400); // 圆角角度 int radius = context.getResources().getDimensionPixelSize(R.dimen.dp_5); // 绘制背景 Drawable drawable = DrawableUtils.roundShapeDrawable(color, alpha, width, height, radius); // 将绘制好的背景转换为Bitmap Bitmap bitmap = DrawableUtils.drawableToBitmap(drawable); // 将转换好的Bitmap设置到ImageView上 views.setImageViewBitmap(R.id.iv_appwidget_bg, bitmap);
特别注意【背景宽度、背景高度】是跟小部件配置文件中的
android:minWidth="@dimen/dp_250"宽度
android:minHeight="@dimen/dp_400"高度
是一致的
小部件配置信息
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/layout_app_widget" android:initialLayout="@layout/layout_app_widget" android:minWidth="@dimen/dp_250" android:minHeight="@dimen/dp_400" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="vertical" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen"/>