首先,我知道有很多类似的问题,但我可以找到一个完整的答案来做我想做的事。它是关于创建自定义组件的。
问题是,我想为我的 UI 创建一个自定义动画组件。此外,客户希望轻松更改组件的图像,因此我决定使用在代码中动画的 xml 布局。但是我好像搞不定...
我第一次尝试的例子是指示二氧化碳水平的针规。
有 2 个图像:仪表和针,所以我用我的两个 ImageView 创建了一个仪表布局。
<?xml version="1.0" encoding="utf-8"?>
<com.erdf.components
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/gauge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/jauge" />
<ImageView
android:id="@+id/needle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/jauge_needle" />
然后我创建了关联的类:
public class components extends RelativeLayout {
private ImageView mNeedle;
private int mValue = 0;
public components(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mNeedle = (ImageView)findViewById(R.id.needle);
}
public void SetGaugeValue(int value) {
if(value > 180) {
value = 180;
}
else if(value < -180)
{
value = -180;
}
RotateAnimation needleDeflection = new RotateAnimation(mValue, value, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
needleDeflection.setDuration(100*(Math.abs(mValue-value)));
needleDeflection.setFillAfter(true);
mNeedle.startAnimation(needleDeflection);
}
}
最后,我尝试将此组件包含在我的 ActivityView 中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
layout="@layout/gauge_layout" />
</RelativeLayout>
这是活动类的代码:
public class GaugeActivity extends Activity {
private components test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_layout);
}
}
所以我的第一个问题是:为什么在执行应用程序时我做错了这个异常:
01-29 16:04:28.886: E/AndroidRuntime(1570): FATAL EXCEPTION: main
01-29 16:04:28.886: E/AndroidRuntime(1570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gauge/com.example.gauge.GaugeActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.os.Looper.loop(Looper.java:137)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-29 16:04:28.886: E/AndroidRuntime(1570): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 16:04:28.886: E/AndroidRuntime(1570): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 16:04:28.886: E/AndroidRuntime(1570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 16:04:28.886: E/AndroidRuntime(1570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 16:04:28.886: E/AndroidRuntime(1570): at dalvik.system.NativeStart.main(Native Method)
01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.createView(LayoutInflater.java:596)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.parseInclude(LayoutInflater.java:807)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-29 16:04:28.886: E/AndroidRuntime(1570): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.Activity.setContentView(Activity.java:1881)
01-29 16:04:28.886: E/AndroidRuntime(1570): at com.example.gauge.GaugeActivity.onCreate(GaugeActivity.java:24)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.Activity.performCreate(Activity.java:5104)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-29 16:04:28.886: E/AndroidRuntime(1570): ... 11 more
01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
01-29 16:04:28.886: E/AndroidRuntime(1570): at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-29 16:04:28.886: E/AndroidRuntime(1570): at java.lang.Class.getConstructor(Class.java:431)
01-29 16:04:28.886: E/AndroidRuntime(1570): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
01-29 16:04:28.886: E/AndroidRuntime(1570): ... 23 more
第二个是关于如何使用我的组件。如何在我的成员变量 test 中转换包含的布局,它是与相同包含的布局关联的类的实例?然后我可以简单地使用 setGaugeValue ( ) 函数来为针设置动画。
我希望我足够清楚。