如何将图像视图设置为画布并在该画布上绘制

时间:2013-12-27 12:14:34

标签: android bitmap android-canvas

我有很多图像,我通过点击一个Button在imageView上设置,我想创建一个Drawing类,这样当图像在ImageView上我可以在上面绘画,点击按钮后我就可以了绘制新图像。 直到现在我有这个。请帮我做这个我是编程的新手我不太了解。关于OOP和DS的一点点。

public class EnglishDrawing extends Activity{
private int currentImage = 0;
private ImageView changeImageView;
private Button changeImageOnClick;
private Button changePreviousImageOnClick;
public Bitmap mybitmap,newbmp,bitmap,bmp;
int image[] = { R.drawable.englishdot_01, R.drawable.englishdot_02,
        R.drawable.englishdot_03, R.drawable.englishdot_04,
        R.drawable.englishdot_05, R.drawable.englishdot_06,R.drawable.englishdot_07,
        R.drawable.englishdot_08,
        R.drawable.englishdot_09,
        R.drawable.englishdot_10,
        R.drawable.englishdot_11,
        R.drawable.englishdot_12,
        R.drawable.englishdot_13,
        R.drawable.englishdot_14,
        R.drawable.englishdot_15,
        R.drawable.englishdot_16,
        R.drawable.englishdot_17,
        R.drawable.englishdot_18,
        R.drawable.englishdot_19,
        R.drawable.englishdot_20,
        R.drawable.englishdot_21,
        R.drawable.englishdot_22,
        R.drawable.englishdot_23,
        R.drawable.englishdot_24,
        R.drawable.englishdot_25,
        R.drawable.englishdot_26

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.englishdrawing);
    changeImageOnClick = (Button) findViewById(R.id.nextDrawEng);
    changePreviousImageOnClick = (Button) findViewById(R.id.preDrawEng);
    changeImageView = (ImageView) findViewById(R.id.englishdrawingimageview);
    changeImageOnClick.setOnClickListener(aButtonChangeImageListener);
    changePreviousImageOnClick.setOnClickListener(bButtonOnClickListener);
    changeImageView.setDrawingCacheEnabled(true);
}
View.OnClickListener aButtonChangeImageListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        currentImage++;
        currentImage = currentImage % image.length;
        changeImageView.setImageResource(image[currentImage]);
    }
};
View.OnClickListener bButtonOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        currentImage--;
        currentImage = (currentImage + image.length) % image.length;
        changeImageView.setImageResource(image[currentImage]);

    }
};

}

1 个答案:

答案 0 :(得分:0)

您需要将图片加载为位图:

 Resources res = getResources();
 Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.your_image);
 canvas.drawBitmap(bitmap , 0,0, null);

然后你可以在画布上画画。 在点击画布时,您可以像上面一样将下一个图像作为位图,然后再在画布上绘制。这也是您在图像上绘制的方式..!