I need to access canvas I created from another class.
So I have a class which extends LinearLayout
drawing arc.
Now from main activity
I need to access this canvas it is drawn on and redraw it. Ideally only some parts of it.
Here are some codes stripped to main.
ArchProgressBar.java
(the one with canvas)
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
main activity for app (extends 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);
}
}
now When BroadcastRecieved
is triggered I want to access ArchProgressBar
's canvas, delete the hour line
(or whole thing if not possible to delete just that part) and redraw something new on it.
if you are wandering whats with drawMeCircle
. It is just something I was playing with. included to show how I think it must work.