java—如何在活动的运行时一步一步地绘制线位图

xuo3flqw  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(385)

我有画线的样本代码。我需要一点一点地画一条线作为运行时间。但现在我在行刑后只看到了活动。但是,我需要开始活动,一点一点地展示这条线。
样本代码

public class MainActivity extends ActionBarActivity {

ImageView drawingImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
    Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawingImageView.setImageBitmap(bitmap);

    // Line
    Paint paint = new Paint();
    paint.setColor(Color.rgb(255, 153, 51));
    paint.setStrokeWidth(10);
    int startx = 50;
    int starty = 90;
    int endx = 350;
    int endy = 90;
    while(endx>=startx) {
        try {
            Thread.sleep(5);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        canvas.drawLine(startx, starty, startx+1, endy, paint);
        startx++;
    }
}}

xml格式

<?xml version="1.0" encoding="utf-8"?>
<ImageView android:id="@+id/DrawingImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
kmb7vmvb

kmb7vmvb1#

当动画到达endx位置时,使用下面的代码重新启动动画-

private void ImgAnimation() {

        mHandlerAnimation = new Handler();

        mRunnableAnimation = new Runnable() {
            public void run() {

                  if(endx>startx) 
                  {
                         canvas.drawLine(startx, starty, startx+1, endy, paint);
                         startx = startx + 1;
                         getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
                  }
                  else
                  {
                      bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                      canvas = new Canvas(bitmap);
                      drawingImageView.setImageBitmap(bitmap);
                      startx = 50;
                  }

                mHandlerAnimation.postDelayed(this, 80);
            }
        };

        mHandlerAnimation.postDelayed(mRunnableAnimation, 80);

    }
3lxsmp7m

3lxsmp7m2#

我认为行添加到您的视图,但用户界面不刷新,所以在线程后添加下面的行。睡眠(5)刷新用户界面

getWindow().getDecorView().findViewById(android.R.id.content).invalidate();

或者你可以使用下面的代码-

public class ImgDraw extends Activity {

    ImageView drawingImageView;
    Handler mHandlerAnimation = null;
    Runnable mRunnableAnimation = null;
     Canvas canvas ;
     int startx = 0,starty =0,endx = 0,endy = 0;
     Paint paint;
     Bitmap bitmap ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imgdraw);

        drawingImageView = (ImageView)findViewById(R.id.DrawingImageView);

          DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
           int width = metrics.widthPixels;
           int height = metrics.heightPixels;

          bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

            canvas = new Canvas(bitmap);
            drawingImageView.setImageBitmap(bitmap);

            // Line
            paint = new Paint();
            paint.setColor(Color.rgb(255, 153, 51));
            paint.setStrokeWidth(10);
             startx = 50;
             starty = 90;
             endx = 350;
             endy = 90;          

          //   canvas.drawLine(startx, starty, startx+1, endy, paint);

                ImgAnimation();

    }

    private void ImgAnimation() {

        mHandlerAnimation = new Handler();

        mRunnableAnimation = new Runnable() {
            public void run() {

                  if(endx>=startx) {
                         canvas.drawLine(startx, starty, startx+1, endy, paint);
                            startx = startx + 10;
                            getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
                     }

                mHandlerAnimation.postDelayed(this, 80);
            }
        };

        mHandlerAnimation.postDelayed(mRunnableAnimation, 80);

    }

}

相关问题