0

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();
            }
        }
    }
}
divibisan
  • 11,659
  • 11
  • 40
  • 58
Xwa
  • 21
  • 5

1 Answers1

0

Thanks to @Deepak kaku, I find a way to solve the problem by the link in the comment. If I use the main thread to update UI, the program will delay and the draw process will stop, and in the next loop the constructed bitmap will be destroyed and another bitmap will be constructed, so the ImageView can only show the last picture. I solve the problem by open anther thread to update the UI I attach the code below:

package com.example.xwa.test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private Bitmap bm;
private Canvas canvasBit;
private Paint paintRect;
private Paint paint;
private Handler handler = new Handler();
private Runnable runnable;
final String TAG="TESTTEST";
String path;
int count;
TextView tv;
int i=1;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        UIinital();
        startdraw();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

void UIinital() throws IOException {
    paintRect = new Paint();
    paintRect.setColor(Color.YELLOW);
    paint = new Paint();
    path="car4/img";
    String[] l=getAssets().list(path);
    count=l.length;
    iv=findViewById(R.id.imageView);
    tv=findViewById(R.id.textView);
}

void startdraw()
{
    runnable=new Runnable(){
        public void run() {
            try {
                handler.postDelayed(this, 1000);
                DecimalFormat d=new DecimalFormat("0000");
                String file=path+"/"+d.format(i)+".jpg";
                InputStream in=getAssets().open(file);
                bm= BitmapFactory.decodeStream(in);
                iv.setImageBitmap(bm);
                Log.i(TAG, "run: "+new Integer(i).toString());
                tv.setText("result="+new Integer(i).toString()+"\n");
                i++;
                if(i==count)
                    handler.removeCallbacks(runnable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 0);
}
protected void onStop() {
    super.onStop();
    handler.removeCallbacks(runnable);
}

}

Xwa
  • 21
  • 5