0

まず第一に、同様の質問がたくさんあることは知っていますが、やりたいことをするための完全な答えを見つけることができました. カスタムコンポーネントの作成についてです。

つまり、UI 用のカスタム アニメーション コンポーネントを作成したいと考えています。さらに、クライアントはコンポーネントの画像を簡単に変更したいので、コードでアニメーション化される xml レイアウトを使用することにしました。でも、どうも上手くいかないらしい…。

私の最初の試みの例は、CO2 レベルを示す針ゲージです。

ゲージと針の 2 つの画像があるため、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>

アクティビティ Class のコードは次のとおりです。

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

2 つ目は、コンポーネントの使用方法についてです。同じ含まれるレイアウトに関連付けられたクラスのインスタンスであるメンバー変数 test に含まれるレイアウトをキャストするにはどうすればよいですか? 次に、setGaugeValue ( ) 関数を使用して針をアニメーション化するだけです。

私が十分に明確だったことを願っています。

4

1 に答える 1

0

「コンポーネント」にカスタム RelativeLayout を使用する必要はありません。通常の RelativeLayout でかまいません。

いずれにせよ、その特定の RelativeLayout への参照を作成します

(components) comp = (components) findViewById(R.id.myComponent)
  //note that you don't have id set up yet in your custom view xml tag

その後、使用できます

comp.SetGaugeValue(5);

クラス名「components」は大文字で始まる必要があることに注意してください - Java practice.

于 2013-01-29T17:32:43.333 に答える