Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “移动”;“云”用手指移动的径向梯度_Android_Android Layout_Ontouchlistener_Radial Gradients - Fatal编程技术网

Android “移动”;“云”用手指移动的径向梯度

Android “移动”;“云”用手指移动的径向梯度,android,android-layout,ontouchlistener,radial-gradients,Android,Android Layout,Ontouchlistener,Radial Gradients,第一个问题,我有工作代码在文件的其他地方进行移动——这不是问题所在。问题是如何创建可以移动的径向渐变(低于API 16) 先发制人,我在这里花了很多时间: 使用GradientDrawable(如下所示),似乎无法在不设置非径向方向的情况下设置颜色 public class CustomView extends View { int width = (sWidth/8); // sWidth defined elsewhere as width of screen int he

第一个问题,我有工作代码在文件的其他地方进行移动——这不是问题所在。问题是如何创建可以移动的径向渐变(低于API 16)

先发制人,我在这里花了很多时间:

使用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);
        }
    }
}
还有一点:

但似乎没有办法改变这种梯度。你能把径向梯度放在一个透明的圆上,然后移动它吗?我不知所措。提前感谢。

编辑:

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

步骤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);
        }
-------------原始的、流程效率较低的解决方案保留在下面-----------

等几周,你就可以回答自己的问题了。结果整个过程都是径向梯度

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会发疯。然而,如果您试图在构造函数中实例化它,事情很快就会变得糟糕

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

如图所示(免费)
            // 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;
        }
    }
}