android - 重绘 View Android

标签 android view

我现在有一个在 onCreate 之后以编程方式添加的 View (取决于其他一些变量)。一切正常,它绘制了一个圆圈的一部分。

但我的问题是我以后如何重新绘制它?获取一些数据后,我需要更改圆圈中的角度。

风玫瑰代码:

    public class WindRose extends View {

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


    }

    @Override

    protected void onDraw(Canvas canvas) {


        super.onDraw(canvas);
        canvasTest = canvas;


        float height = (float) getHeight();
        float width = (float) getWidth();

        float radius;

        if (width > height) {
            radius = height / 2;

        } else {
            radius = width / 2;
        }

        // radius = (height )/ 2;


        Path path = new Path();
        path.addCircle(width, height, radius, Path.Direction.CCW);

        // / 2

        Resources resources = getResources();
        int color = resources.getColor(R.color.green_back);



        paint.setColor(color);
        paint.setStrokeWidth(5);

        paint.setStyle(Paint.Style.FILL);
        float center_x, center_y;
        center_x = width / 2;
        center_y = height / 2;



        //Formulas :
        //SD = Start Degree
        //ED = End Degree

        //If cakepiece passes 0 (East)
        //SD, 360-(SD+ED)

        //Else :
        //SD, (ED-SD)

        oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius);

        if (End > Start) {
            canvas.drawArc(oval, Start, (End - Start), true, paint);

        } else if (End < Start) {
            canvas.drawArc(oval, Start, ((360 - Start) + End), true, paint);
        }


    }


}

如果我更新 StartEnd 变量,什么也不会发生。我也尝试在类里面调用 invalidate 但我也没有得到任何重绘。

使我尝试过的无效:

WindRose windrose = new WindRose(this);
windrose.invalidate();

从 WindRose 所属的主类调用。

我应该如何正确执行此操作?

最佳答案

也许调用 View 的 invalidate() 方法会有帮助。

您可以在此处阅读更多内容 ( http://developer.android.com/reference/android/view/View.html ),但是: “要强制绘制 View ,请调用 invalidate()。”

请注意,您只能使 View 的一部分无效

关于android - 重绘 View Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233221/

相关文章:

android - fragment 如何在 Android Talkback 中启动时宣布自己?

Android 如何重用标题布局作为 ListView 中的空 View

android - 从放置在 ListView 中的编辑文本获取数据

java - 如何限制 fragment 中的后退 Action

java - 如何从另一个线程使 View 无效?

sql - 甲骨文:选择并行条目

android - 在 Android 中动态添加 LinearLayouts

mysql - 使用 EXIST 命令创建 sql View 语句

android - 获取选中复选框的位置

sql-server - Entity Framework ,查看而不是插入触发器。无法在 View 中插入一行