Shu*_*uri 2 android live-wallpaper android-canvas
如何在图像上应用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);
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西

PS我正试图为动态壁纸做这个.
san*_*tar 10
可能只有视图操作才能满足所需的行为.对于它你应该:
RES /绘制/ 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>
Run Code Online (Sandbox Code Playgroud)
定义布局: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" />
Run Code Online (Sandbox Code Playgroud)
这里drawable/photo只是jableg in drawables文件夹;
有人建议将以下代码添加到活动中(实际上,它取决于设备本机配置,对于现在> 3.0设备似乎是多余的):
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
Run Code Online (Sandbox Code Playgroud)之后我在4.1设备上观察到以下内容:

它看起来像渐变.我还不确定这是不是你想要的.
| 归档时间: |
|
| 查看次数: |
8725 次 |
| 最近记录: |