Custom android AnalogClock from original AnalogClock class not working
I want to make a custom AnalogClock class using source code of AnalogClock by android developers that is available in the internet.
I want to make the clock set the time that I want, not the current time. I didn't find a clear example on how to do it so maybe this post will be useful. After copying source code to new file, I get some errors. Below is the original source code of AnalogClock:
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews.RemoteView;
import java.util.TimeZone;
/**
* This widget display an analogic clock with two hands for hours and
* minutes.
*/
@RemoteView
public class AnalogClock extends View {
private Time mCalendar;
private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mDial;
private int mDialWidth;
private int mDialHeight;
private boolean mAttached;
private final Handler mHandler = new Handler();
private float mMinutes;
private float mHour;
private boolean mChanged;
public AnalogClock(Context context) {
this(context, null);
}
public AnalogClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
!!Error mContext -> change to context
public AnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = mContext.getResources();
!!Error com.android.internal.R cannot be resolved. I found a solution to this problem with
mDial = r.getDrawable(Resources.getSystem().getIdentifier("clock_dial","drawable", "android"));
And same for other lines just changing name and either drawable or styleable.
vvvv----But I still have a problem with the below code.
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
^^^^----I don't have idea how to change it and make it work
mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
if (mDial == null) {
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
I want to make a custom AnalogClock class using source code of AnalogClock by android developers that is available in the internet.
I want to make the clock set the time that I want, not the current time. I didn't find a clear example on how to do it so maybe this post will be useful. After copying source code to new file, I get some errors. Below is the original source code of AnalogClock:
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews.RemoteView;
import java.util.TimeZone;
/**
* This widget display an analogic clock with two hands for hours and
* minutes.
*/
@RemoteView
public class AnalogClock extends View {
private Time mCalendar;
private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mDial;
private int mDialWidth;
private int mDialHeight;
private boolean mAttached;
private final Handler mHandler = new Handler();
private float mMinutes;
private float mHour;
private boolean mChanged;
public AnalogClock(Context context) {
this(context, null);
}
public AnalogClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
!!Error mContext -> change to context
public AnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = mContext.getResources();
!!Error com.android.internal.R cannot be resolved. I found a solution to this problem with
mDial = r.getDrawable(Resources.getSystem().getIdentifier("clock_dial","drawable", "android"));
And same for other lines just changing name and either drawable or styleable.
vvvv----But I still have a problem with the below code.
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
^^^^----I don't have idea how to change it and make it work
mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
if (mDial == null) {
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
No comments:
Post a Comment