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

3

首先Q。我已经有了能够在文件中其他位置移动的工作代码——这不是问题。问题是如何创建一个可以移动的径向渐变(低于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个回答

2

编辑:

步骤1,在drawable文件夹中定义一个椭圆形状。这个是“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>

半径、宽度和高度可能需要动态更改。所以随便放置。上面的颜色方案将给予略带透明的颜色到完全透明。云朵效果。

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

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

// now the constructor:

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

第三步,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);
        }

------------- 原始、效率低下的解决方案如下 -----------

等几周,你就能回答自己的问题了。结果发现一直都是径向渐变。

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会感到不满。然而,如果您尝试在构造函数中实例化它,事情会变得非常丑陋。

如果能避免上述问题,则可以额外获得奖励。


如此看来,https://play.google.com/store/apps/details?id=com.anthropomo.digitalrattle(免费) - anthropomo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接