我在绘制ImageView时遇到了一些问题 当我按下按钮(调用函数zxc())时,程序会在错误的位置绘制 为什么呢?
屏幕直至您按下:http://s52.radikal.ru/i137/1401/51/c084b1b955d9.png
之后:http://s020.radikal.ru/i718/1401/b6/bec1187422ca.png
代码:
package com.example.asd;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
Bitmap myBitmap;
ImageView myImageView;
Canvas myCanvas;
Resources myResources;
Context myContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageView=(ImageView)findViewById(R.id.imageView1);
myBitmap=Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
myImageView.setImageBitmap(myBitmap);
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
myBitmap.setPixel(i, j, 0xffff4000);
myContext=getWindow().getContext();
myResources=myContext.getResources();
Drawable myDrawable=myResources.getDrawable(R.drawable.qwe1);
myDrawable.setBounds(10, 10, 100, 100);
myCanvas=new Canvas(myBitmap);
myImageView.setImageDrawable(myDrawable);
myImageView.draw(myCanvas);
myImageView.setImageBitmap(myBitmap);
Drawable my_second_draw=myResources.getDrawable(R.drawable.qwe1);
my_second_draw.setBounds(900, 900, 1000, 1000);
myImageView.setImageDrawable(my_second_draw);
myImageView.draw(myCanvas);
myImageView.setImageBitmap(myBitmap);
}
public void zxc(View v)
{
Drawable my_second_draw=myResources.getDrawable(R.drawable.ic_launcher);
my_second_draw.setBounds(500, 500, 1000, 1000);
myImageView.setImageDrawable(my_second_draw);
myImageView.draw(myCanvas);
myImageView.setImageBitmap(myBitmap);
}
}