Recently I am working on a program on android
, I have a set of pictures and I extract feature from them and draw shapes on them according to their feature. And I want the modified picture be showed on the view one by one like a video. I convert each picture to bitmap and load it into a Canvas
then I draw the shape on the Canvas
. I use ImageView
to show the Canvas
and make the thread sleep 1s before I make another painting.
But after the process, the view only show the last picture. I want to know why and how to solve it?
The code below is that I try to do it using self-defined View, the result is the same as ImageView.setImageBitmap, just showed the last picture.
public class MainActivity extends AppCompatActivity {
final String TAG="TEST";
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate: ");
setContentView(new Myview(this));
}
class Myview extends View{
private int width;
private int height;
private Bitmap bitmap;
private Canvas canvasBit;
private Paint paintCircle;
private Paint paintRect;
private Paint paint;
private int bimapWidth;
private int bitmapHeight;
public Myview (Context context) {
super(context);
paintCircle = new Paint();
paintCircle.setColor(Color.YELLOW);
paintCircle.setStyle(Paint.Style.FILL);
paintCircle.setAntiAlias(true);
paintRect = new Paint();
paintRect.setColor(Color.GREEN);
paint = new Paint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
canvasBit=new Canvas(bitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
String path="car4/img";
DecimalFormat d=new DecimalFormat("0000");
int count=0;
try {
String[] l= new String[0];
l = getAssets().list(path);
count=l.length;
for(int i=1;i<=5;i++)
{
String file=path+"/"+d.format(i)+".jpg";
InputStream in=getAssets().open(file);
Bitmap bm= BitmapFactory.decodeStream(in);
canvas.drawBitmap(bm,0,0,paint);
canvas.drawCircle(10,10,50,paintCircle);
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}