2012-10-25 28 views
3

首先問:我有工作代碼來使其在文件中的其他位置移動 - 這不是問題。問題是如何創建可移動的徑向漸變(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

但似乎沒有辦法來移動漸變。你可以把徑向漸變放在某種可以移動的透明圓上嗎?我很茫然。我提前感謝。

回答

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會生氣。但是,如果你嘗試在構造函數中實例化它,事情會變得很難。

避免上述問題的解決方案的額外要點。

+0

如此處所示https://play.google.com/store/apps/details?id=com.anthropomo.digitalrattle(免費) – anthropomo