【问题标题】:Moving "Cloud" - Radial Gradient moving with finger移动“云” - 随手指移动的径向渐变
【发布时间】:2012-10-25 14:40:39
【问题描述】:

第一个问题。我有工作代码可以在文件的其他位置进行此移动——这不是问题所在。问题是如何创建可以移动的径向渐变(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

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

【问题讨论】:

    标签: android android-layout ontouchlistener radial-gradients


    【解决方案1】:

    编辑:

    第 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>
    

    半径、宽度和高度可能需要动态更改。所以随便放。上面的配色方案会给完全透明的稍微透明的颜色。云效果。

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

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

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

    【讨论】:

    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多