动画 onDraw() 部分

发布于 2024-11-10 16:14:24 字数 1758 浏览 8 评论 0原文

我有以下课程:

public class MainActivity extends Activity {
/** Called when the activity is first created. */

String value = "0";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(new GaugeAnimation(this));

       }

     }

我的 GuageAnimation 课程如下:

public class GaugeAnimation extends View{

private Path p;
private Paint cPaint = new Paint();
private int width = 200;
private int angleStart = 135;
private int sweep = 90;
private int value=0;

Bitmap bottom = BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_bottom);
Bitmap top= BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_active);

public GaugeAnimation(Context context){
    super (context);

        //Arc Equations & etc.... 

}

@Override
public void onDraw(Canvas c){


    Paint paint = new Paint();
    paint.setFilterBitmap(false);
    paint.setColor(Color.BLUE);
    c.translate(55,320);       

    //Draw bottom image on canvas

  c.save();

    p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);


    c.clipPath(p);

    //draw Image on top

    c.restore();

   invalidate()

}


 }

所以基本上,这会根据弧方程一次裁剪一张圆形图像。我想显示一个动画,就像圆圈的各个部分都被填充一样(因此显示每个剪切路径,演示正在构建的圆圈)。所以我正在考虑做一个 for 循环,例如:

 for (int i=0; i<100; i++) {
   sweep=i;
   p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);

    c.clipPath(p);
        invalidate();
 }

但这不起作用,它只是在 i=100 时绘制最终结果,有人知道我该如何做到这一点吗?

I have the following class:

public class MainActivity extends Activity {
/** Called when the activity is first created. */

String value = "0";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(new GaugeAnimation(this));

       }

     }

My GuageAnimation class is as follows:

public class GaugeAnimation extends View{

private Path p;
private Paint cPaint = new Paint();
private int width = 200;
private int angleStart = 135;
private int sweep = 90;
private int value=0;

Bitmap bottom = BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_bottom);
Bitmap top= BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_active);

public GaugeAnimation(Context context){
    super (context);

        //Arc Equations & etc.... 

}

@Override
public void onDraw(Canvas c){


    Paint paint = new Paint();
    paint.setFilterBitmap(false);
    paint.setColor(Color.BLUE);
    c.translate(55,320);       

    //Draw bottom image on canvas

  c.save();

    p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);


    c.clipPath(p);

    //draw Image on top

    c.restore();

   invalidate()

}


 }

so basically this crops a circular image one piece at a time based on arc equations. I want to show an animation like the circle's pieces all being filled in (so showing each clipped path which demonstrates a circle being built). So i was thinking of doing a for loop like:

 for (int i=0; i<100; i++) {
   sweep=i;
   p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);

    c.clipPath(p);
        invalidate();
 }

but this doesn't work it just draws the end result when i=100, anyone have an idea as to how i can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2024-11-17 16:14:24

目前,您的循环在主线程上执行并使其保持忙碌状态,因此在您完成之前它不会实际更新 UI。

您应该在后台线程中执行循环(使用 TimerAsyncTask) 并在主线程上执行绘制。请注意,如果没有短暂的睡眠,它可能看起来太像动画了,速度会相当快。

Currently your loop is performed on the main thread and keeps it busy, so it will not actually update the UI until you finish.

You should do the loop in a background thread (using Timer or AsyncTask) and perform the paint on main thread. Note that without a short sleep, it will probably would look like an animation too much, it will be pretty fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文