移动“云” - 用手指移动径向渐变

时间:2012-10-25 14:40:39

标签: android android-layout ontouchlistener radial-gradients

首先问:我有工作代码在文件的其他地方进行此操作 - 这不是问题。问题是如何创建可以移动的径向渐变(低于API 16)。

抢先一试,我在这里度过了很多时间:

http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

使用GradientDrawable(下图),似乎没有办法设置颜色而不设置非径向方向。

public class CustomView extends View {
    int width = (sWidth/8); // sWidth defined elsewhere as width of screen
    int height = (sWidth/8);
    GradientDrawable gradient;
    int[] colors = {0x60ffffff,0x000000};

    public CustomView(Context context) {
        super(context);
        gradient = new GradientDrawable(GradientDrawable.Orientation.BL_TR,colors);
    }

    protected void onDraw(Canvas canvas) {
        if(x != 0 && y != 0){ // OnTouch calls invalidate on this view for movement
            gradient.mutate();
            gradient.setShape(GradientDrawable.RADIAL_GRADIENT);
         // This just makes it disappear:
         // setGradientType (GradientDrawable.RADIAL_GRADIENT);
            gradient.setBounds(x-width/2, y-height/2, x + width, y + height);
            gradient.draw(canvas);
        }
    }
}

还有:

http://developer.android.com/reference/android/graphics/RadialGradient.html

但似乎没有办法移动那个渐变。你可以将径向渐变放在某种可以移动的透明圆上吗?我不知所措。我提前感谢。

1 个答案:

答案 0 :(得分:2)

编辑:

步骤1,在可绘制文件夹中定义椭圆形状。这个是“cloud.xml”:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<gradient
    android:centerX="0.5"
    android:centerY="0.5"
    android:endColor="#00000000"
    android:gradientRadius="30"
    android:startColor="#f0ffffff"
    android:type="radial" />
<size
    android:width="60dp"
    android:height="60dp" />
 </shape>

可能需要动态更改半径,宽度和高度。所以放点什么。上面的配色方案会使透明的颜色略微透明。云效应。

第2步,自定义视图的构造函数:

// actually before the constructor this, of course:
GradientDrawable circle;

// now the constructor:

circle = (GradientDrawable) context.getResources().getDrawable(R.drawable.cloud);

第3步,onDraw方法:

            // x & y being coordinates updated from onTouch method,
            // circleRad being some constant dependent on screen dp
            if(x != 0 && y != 0){
            circle.setGradientRadius(circleRad);
            circle.setBounds(x-circleRad, y-circleRad,
                    x+circleRad, y+circleRad);
            circle.draw(canvas);
        }

-------------原创,低效的流程高效解决方案保存在下面-----------

等几周,你可以回答自己的问题。事实证明它一直是RadialGradient。

public class CustomView extends View implements OnTouchListener {
    Shader radialGradientShader;
    Paint paint;
    private int circleDiam;
    private int x = 0;
    private int y = 0;
    private int lastScreenColor;

    public CustomView(Context context, int circleDiam) {
        super(context);
        this.circleDiam = circleDiam;
        paint = new Paint();
    }

    protected void onDraw(Canvas canvas) {
        if(x != 0 && y != 0){       
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            radialGradientShader = new
    RadialGradient(x, y, circleDiam,
    0xf0ffffff,0x00000000,Shader.TileMode.MIRROR);
            paint.setShader(radialGradientShader);
            canvas.drawCircle(x, y, circleDiam, paint);
        }
    }

    public boolean onTouch(View v, MotionEvent event) {
        x = (int)event.getX();
        y = (int)event.getY();

        if(event.getAction() == MotionEvent.ACTION_DOWN 
     && event.getAction() == MotionEvent.ACTION_MOVE){
            invalidate();
            return true;
        }
        else{ 
            x = 0;
            y = 0;
            invalidate();
            return false;
        }
    }
}

蓬松的云!

这个解决方案的唯一问题是当你在onDraw方法中实例化一个对象时Eclipse会生气。但是,如果你试图在构造函数中实例化它,那么事情就会变得很难看。

避免上述问题的解决方案的额外分数。