1

これは、機能するものです:

a)main.xmlに2つのImageViewがあるFrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/frameLayout1"
    android:layout_height="wrap_content">

    <ImageView android:src="@drawable/radar_background"
        android:id="@+id/background" android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ImageView>

    <ImageView android:src="@drawable/radar_sector" android:id="@+id/sector"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>

b)バックグラウンドでRotateAnimationを実行しますが、フォアグラウンドセクターは変更されません。

背景画像に対してもう少し行う必要があるため、これをFrameLayoutサブクラスに入れ、それに応じてmain.xmlを変更しました。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/frameLayout1"
    android:layout_height="wrap_content">

    <com.decades.SensorTest.RadarView
        android:id="@+id/background" android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
    <ImageView android:src="@drawable/radar_sector" android:id="@+id/sector"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>

これは新しいRadarView.javaです。

public class RadarView extends FrameLayout {

    private Bitmap mRadar;

    public RadarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadarView(Context context) {
        super(context);
        init();
    }
    private void init() {
        mRadar = BitmapFactory.decodeResource(getResources(), R.drawable.radar_background);
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawBitmap(mRadar, 0, 0, null);
    }
}

何が起こるのですか:

a)コンストラクターはsetContentView(R.layout.main);の間に呼び出されます。

b)dispatchDrawオーバーライドが呼び出されます

c)画像が画面に表示されない...

誰か見ますか、なぜですか?

4

4 に答える 4

1

代わりにonDraw()を実装してみましたか?

dispatchDraw() は、子ビューと関係があります。

于 2011-03-04T17:11:03.503 に答える
0

Viewクラスを拡張し、onMeasure()メソッドとonDraw()メソッドをオーバーライドする必要あると思います。

詳細はこちら:
http ://developer.android.com/guide/topics/ui/custom-components.html

例:
http ://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/

于 2011-03-04T17:15:07.163 に答える
0

onDraw() を実装したかったと思いますが、おそらくそれだけでは十分ではありません。私の理解では、子供以外に描画するものがない場合、レイアウトは onDraw を呼び出さない可能性があります。レイアウト自体を描画するには、レイアウトを「だます」必要がある場合があります (そのため、onDraw 呼び出しが行われます)。簡単ではあるが効率的ではない方法 (当て推量を含む) は、レイアウトの背景色を設定することです。

より良い方法は、おそらくレイアウトコードを読んで適切に実行することです:)

于 2013-06-24T11:04:53.260 に答える