android - Moving "Cloud"- 随手指移动的径向渐变

标签 android android-layout ontouchlistener radial-gradients

第一个问题。我有工作代码可以将此移动到文件中的其他位置——这不是问题所在。问题是如何创建可以移动的径向渐变(低于 API 16)。

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

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 步,在您的可绘制文件夹中定义一个椭圆形。这是“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 步,自定义 View 的构造函数:

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

------------ 原始的、流程效率较低的解决方案保留在下面 ----------

等待几周,您就可以回答自己的问题了。结果一直都是 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 会生气。但是,如果您尝试在构造函数中实例化它,事情就会变得很快。

为避免上述问题的解决方案加分。

关于android - Moving "Cloud"- 随手指移动的径向渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071060/

相关文章:

android - 如何在 Android 中限制 Spinner 下拉 View 的高度

java - OnTouchListener 没有被解雇

java - ListView 中的 Android Spinner 有时会重新初始化

android - 在android中更新ProgressBar

java - 如何在一次触摸后停止 OnTouch 方法

android - 在 Activity 中查找根布局

可点击元素的 ListView 中的 Android LongClick

android - dp 单位不缩放布局在不同的屏幕

android - Flutter-单击按钮时在GridView中添加容器(窗口小部件)?

Android:示例数据子目录未显示在 Android Studio 3.X 中