使图像产生淡入淡出效果创造阿尔法渐变 [英] Create alpha gradient on image to create fade effect

查看:259
本文介绍了使图像产生淡入淡出效果创造阿尔法渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何申请alpha渐变的图像上,这样它消失线? 现在,我要创建单元宽度的矩形,并用它来绘制位图用油漆对象与α值在一个循环中被改变。我只是做了它,因为我想不出别的。因此,一个更合适的方法会更好。

 位图位= BitmapFactory.de codeStream(是);
BMP位= Bitmap.createScaledBitmap(位图,100,151,TRUE));
bitmap.recycle();

矩形Rect1的=新的Rect(0,0,100,100);
矩形RECT2 =新的Rect(100,0,101,100);

涂料粉刷=新的油漆();

canvas.drawBitmap(BMP,Rect1的,Rect1的,NULL);
而(paint.getAlpha()!= 0){
    paint.setAlpha(paint.getAlpha() -  5);
    canvas.drawBitmap(BMP,RECT2,RECT2,油漆);
    Rect2.set(Rect2.left + 1,Rect2.top,Rect2.right + 1,Rect2.bottom);
}
 

像这样

P.S。我试图做到这一点的一个活的壁纸。

解决方案

也许,所需的行为可能是只景色操作满足。 对于它,你应该:

  • prepare形状绘制

RES /绘制/ gradient_shape.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <梯度
            机器人:startColor =#00FFFFFF
            机器人:endColor =#FFFFFFFF
            机器人:TYPE =线性/>
< /形状>
 

  • 定义布局: activity_main.xml

     < ImageView的
        机器人:ID =@ + ID /摄
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@可绘制/摄
        机器人:SRC =@可绘制/ gradient_shape/>
     

    在这里绘制/照片是刚刚的JPEG可绘制文件夹;

  • 有的建议增加以下code的活性(它实际上取决于设备的本地配置,似乎是多余的了时下> 3.0的设备):

     公共无效onAttachedToWindow(){
         super.onAttachedToWindow();
         窗口窗口= getWindow();
         window.setFormat(PixelFormat.RGBA_8888);
     }
     

在我所观察到的以下我4.1的设备:

它看起来像梯度。我仍然不知道这是否就是你要寻找的。

How can I apply an alpha gradient on an image so that it fades linearly? Right now, I'm creating rectangles of unit width and using it to draw the bitmap with a paint object with alpha value being changed in a loop. I only did it since I couldn't think of anything else. So a neater way would be better.

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);
}

Something like this

P.S. I'm trying to do this for a live wallpaper.

解决方案

Probably, desired behaviour could be meet with only views manipulations. For it You should:

  • Prepare shape drawable

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>

  • Define layout: 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" />
    

    here drawable/photo is just jpeg in drawables folder;

  • Some suggests to add the following code to the activity (it, actually, depends on device native configuration and seems to be redundant for nowadays > 3.0 devices):

    public void onAttachedToWindow() {
         super.onAttachedToWindow();
         Window window = getWindow();
         window.setFormat(PixelFormat.RGBA_8888);
     }
    

After that I've observed the following on my 4.1 device:

It looks like gradient. I'm still not sure if it's what You're looking for.

这篇关于使图像产生淡入淡出效果创造阿尔法渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆