2012-02-19 81 views
2

起初我PROGRAMM看起來是這樣的:如何禁用invalidate()?

enter image description here

但是當我移動屏幕周圍PROGRAMM畫面變成:

enter image description here

中的onDraw

紅色曲線是隨機生成方法。所以我想拖動周圍的照片,並有靜態的背景,而不是在im動畫時失效。

這是我的代碼: 定製視圖: package com.graph.base;

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.AttributeSet; 
import android.view.View; 
import java.util.Random; 

public class CustomView extends View 
{ 
Random random=new Random(); 
public CustomView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomView(Context context) { 
    super(context); 
} 
public void onDraw (Canvas canvas) 
{ 
    super.onDraw(canvas); 
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.WHITE); 
    canvas.drawPaint(paint); 
    Path path=new Path(); 
    path.moveTo(10, 10); 
    for (float i=0.5f; i<=140; i+=10) 
    { 
     path.quadTo(10+i*5, 10+random.nextInt(500), 10+(i+10)*5,  10+random.nextInt(500)); 
    } 
    paint.setDither(true); 
    paint.setColor(Color.RED); 
    paint.setStyle(Paint.Style.STROKE);  
    paint.setStrokeJoin(Paint.Join.ROUND); 
    paint.setStrokeCap(Paint.Cap.ROUND); 
    paint.setStrokeWidth(2); 
    canvas.drawPath(path, paint); 
} 

}

MainActivity

package com.graph.base; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 
FrameLayout frame; 
ImageView image; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    frame=(FrameLayout)findViewById(R.id.frameLayout1); 
    image=(ImageView)findViewById(R.id.imageView1); 
    image.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 
      Log.e("TAG", "Touch happend"); 
      switch (event.getAction()) 
      { 
      case MotionEvent.ACTION_MOVE: 
      { 
       image.setPadding((int)event.getX(),  (int)event.getY(), 0, 0); 
       break; 
      } 

      } 

      return true; 
     } 
    }); 
} 

public void invalidateButton_Click (View v) 
{ 
    frame.invalidate(); 
} 
} 

回答

0

您正在繪製線條隨意在畫布。每次屏幕失效時都會調用此代碼。如果您想要保持圖形不變,請將位圖設置爲畫布並繪製到位圖中。在你的onDraw()方法中,你將繪製位圖到畫布上。

// Outside of onDraw() 
Bitmap bmDest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
Canvas mycanvas = new Canvas(bmDest); 
// Do your drawing on the canvas outside of the onDraw() method 

在的onDraw()中,只要做到這一點:

canvas.drawBitmap(bmDest, x, y, null); 
+0

無法在此處發佈代碼,但是當我嘗試將我的自定義視圖的構造函數的所有onDraw方法重寫時發生錯誤。我認爲,當我創建我的視圖時,我生成圖形,將它放到位圖畫布上,然後繪製,但是我得到了很多錯誤。 – 2012-02-19 20:08:03

+0

這裏是我的代碼:[鏈接](http://pastebin.com/D6CaHWVP) – 2012-02-19 20:17:00

+0

除了drawBitmap()之外,快速查看代碼看起來沒問題。這兩個數字是左上角的x/y,而不是寬度/高度。你沒有告訴我錯誤是什麼,所以我不能發表評論。您確實需要正確設計繪圖/繪畫邏輯,以便隨意使用。 – BitBank 2012-02-19 20:42:11

0

其實,你不應該 「禁用無效」,但確實叫 「無效」 ......

case MotionEvent.ACTION_MOVE: 
     { 
      image.setPadding((int)event.getX(),  (int)event.getY(), 0, 0); 
      // here call frame.invalidate(Rect dirty), with dirty set to where the image was previously 
      break; 
     } 

請注意,如果您的自定義視圖繪製「隨機」,那麼你就可以得到緩存版本中的位圖和畫從該(blit副本)