如何在图像上应用 alpha 渐变以使其线性淡化? 现在,我正在创建单位宽度的矩形,并用它来绘制带有 alpha 值在循环中更改的绘制对象的位图。我只是这样做了,因为我想不出别的。所以更简洁的方法会更好。
Bitmap bitmap = BitmapFactory.decodeStream(is);
Bitmap bmp = Bitmap.createScaledBitmap(bitmap, 100, 151, true));
bitmap.recycle();
Rect Rect1 = new Rect(0, 0, 100, 100);
Rect Rect2 = new Rect(100, 0, 101, 100);
Paint paint = new Paint();
canvas.drawBitmap(bmp, Rect1, Rect1, null);
while (paint.getAlpha() != 0) {
paint.setAlpha(paint.getAlpha() - 5);
canvas.drawBitmap(bmp, Rect2, Rect2, paint);
Rect2.set(Rect2.left + 1, Rect2.top, Rect2.right + 1, Rect2.bottom);
}
像这样
附言我正在尝试为动态壁纸执行此操作。
最佳答案
可能,仅通过 View 操作即可满足所需的行为。 为此你应该:
- 准备可绘制的形状
res/drawable/gradient_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#00FFFFFF"
android:endColor="#FFFFFFFF"
android:type="linear" />
</shape>
定义布局:activity_main.xml:
<ImageView android:id="@+id/photo" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/photo" android:src="@drawable/gradient_shape" />
这里的 drawable/photo 只是 drawables 文件夹中的 jpeg;
有人建议将以下代码添加到 Activity 中(实际上,它取决于设备 native 配置,并且对于现在 > 3.0 的设备来说似乎是多余的):
public void onAttachedToWindow() { super.onAttachedToWindow(); Window window = getWindow(); window.setFormat(PixelFormat.RGBA_8888); }
在那之后,我在我的 4.1 设备上观察到以下内容:
看起来像渐变。我仍然不确定这是否是您要找的。p>
关于android - 在图像上创建 alpha 渐变以创建淡入淡出效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151727/