来自不同类的Android访问画布

时间:2016-10-24 19:46:17

标签: java android android-layout android-canvas android-relativelayout

我需要访问我从另一个类创建的画布。

所以我有一个扩展LinearLayout绘制弧的类。

现在从main activity我需要访问这个画布,然后重绘它。理想情况下只是它的某些部分。

以下是一些剥离为主要的代码。

ArchProgressBar.java(带画布的那个)

public class ArchProgressBar extends LinearLayout {

    public ArchProgressBar(Context context, AttributeSet attrs) {

    private void init(Context context) {
        this.setWillNotDraw(false);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.arch_progress_bar, this, true);
        this.postInvalidate();
    }

    static void drawMeCircle(int aka) {

    }

    @Override
    public void onDraw(Canvas canvas) {

        int mPaintHex = 0xFFFFFFFF;
        int mPaintColor = Color.argb(100, Color.red(mPaintHex), Color.green(mPaintHex), Color.blue(mPaintHex));

        float left = 295;
        float top = 712;
        float right = 788;
        float bottom = 1208;

        Paint mPaintBackground = new Paint();
        mPaintBackground.setAntiAlias(true);
        mPaintBackground.setStyle(Paint.Style.STROKE);
        mPaintBackground.setStrokeWidth(13);
        mPaintBackground.setColor(mPaintColor);

        RectF mRectF = new RectF(left, top, right, bottom);

        // THIS IS BACKGROUND LINE! draw background line
        canvas.drawArc(mRectF, START_ANGLE, ARCH_LENGTH, false, mPaintBackground);

        int mHourHex = 0xFF00FF00;
        int mHourColor = Color.argb(255, Color.red(mHourHex), Color.green(mHourHex), Color.blue(mHourHex));

        Paint mPaintHours = new Paint();
        mPaintHours.setAntiAlias(true);
        mPaintHours.setStyle(Paint.Style.STROKE);
        mPaintHours.setStrokeWidth(13);
        mPaintHours.setColor(mHourColor);

        RectF mRectH = new RectF(left, top, right, bottom);

        // THIS IS HOUR LINE!draw hour line
        Calendar hCal = Calendar.getInstance();
        int hHour = hCal.get(Calendar.HOUR);
        if(hHour != 0) {
            for(int i=0; i<hHour-1; i++){
                canvas.drawArc(mRectH, START_ANGLE + ARCH_LENGTH + (i*30) +2, ARCH_LENGTH, false, mPaintHours);
            }
        }
    }
应用的

HomeActivity.java主要活动(扩展Activity

public class HomeActivity extends Activity {

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

        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(Intent.ACTION_TIME_TICK);
        mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        registerReceiver(m_timeChangedReceiver, mIntentFilter);
    }
    //clock thing start
    private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
                Calendar hCal = Calendar.getInstance();
                int hHour = hCal.get(Calendar.HOUR);
                ArchProgressBar.drawMeCircle(1);
        }
    };

    public void showApps(View v){
        Intent i = new Intent(this, AppsListActivity.class);
        startActivity(i);
    }
}

现在当BroadcastRecieved被触发时,我想要访问ArchProgressBar的画布,删除hour line(如果不能删除整个部分,则删除整个部分)并重绘新事物。

如果你和drawMeCircle徘徊。这只是我玩的东西。包含以显示我认为它必须如何工作。

1 个答案:

答案 0 :(得分:1)

您只需更改视图对象中的某个值,然后调用invalidate()即可获得所需内容。这将尽快重绘画布。关于您的代码我建议您以这种方式对其进行修改(假设您的视图有id = @ + id / arch_progress_bar)

<强> HomeActivity

private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Calendar hCal = Calendar.getInstance();
        int hHour = hCal.get(Calendar.HOUR);
        ArchProgressBar bar = (ArchProgressBar) findViewById(R.id.arch_progress_bar);
        if (bar != null) {
            bar.drawMeCircle(1);
        }
    }
};

// remember to unregister the broadcast receiver in your onDestroy()
// as further optimization, you can also move the findViewById inside your onCreate(), so you don't have to repeat it for every broadcast you get

<强> ArchProgressBar

pubic void drawMeCircle(int aka) {
     // your business logic
     invalidate();
}   // Remove the static keyword, it's not necessary

最后一个考虑:由于显示旋转,你应该照顾活动娱乐。要管理这种情况,您应该在onSaveInstanceState()活动回调期间保存从广播接收器获得的值(在此示例中为1?),通过您获得的onCreate参数包将其检索并再次设置在你看来。