다른 클래스에서 만든 캔버스에 액세스해야합니다.다른 클래스의 Android 액세스 캔버스
그래서 나는 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
주요 활동 지금 BroadcastRecieved
가 나는 ArchProgressBar
의 캔버스에 액세스하려면 트리거되면 (확장 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);
}
}
의 삭제 hour line
(또는 해당 부분 만 삭제할 수없는 경우 모든 항목) 새 항목을 다시 그립니다.
drawMeCircle
으로 무엇을 배회하고 있습니까? 그것은 내가 가지고 놀고있는 것입니다. 어떻게 작동해야한다고 생각하는지 보여주기 위해 포함되었습니다.
BroadcastReceiver는 어디에 등록합니까? –
@ Code-Apprentice I 코드 업데이트 – CBeTJlu4ok
[BroadcastReceiver에서 UI를 업데이트하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/14643385/how-to-update-ui-in-a-broadcastreceiver) –