2011-08-01 10 views
0

Viewクラスを拡張しているときに問題が発生していますが、XMLはImageViewで試してみましたが正常に動作します...LayerDrawableで何も表示しないカスタムビューでの問題

(RES /描画可能-MDPIフォルダ内)

BubbleView.java

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Canvas; 
import android.graphics.drawable.LayerDrawable; 
import android.view.View; 

public class BubbleView extends View { 

    LayerDrawable rootBubble; 

    public BubbleView(Context context) {   
      super(context); 

      Resources res = this.getResources(); 
      rootBubble = (LayerDrawable) res.getDrawable(R.drawable.bubble); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      rootBubble.draw(canvas); 

    } 


} 

bubble.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape 
    android:shape="rectangle"> 
     <solid android:color="#FFFF0000" /> 
     <size android:width="100px" android:height="100px" /> 
     <padding android:bottom="1dp"/> 
    </shape> 
</item> 

<item> 
    <shape 
    android:shape="oval"> 
     <solid android:color="#FFFFFFFF" /> 
     <size android:width="100px" android:height="100px" /> 
    </shape> 
</item> 

</layer-list> 

主活性

public class VisualManagerActivity extends Activity { 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LinearLayout main = new LinearLayout(this);  
    main.addView(new BubbleView(this));  
    setContentView(main); 
    } 
} 

ありがとうございました!

答えて

2

LayerDrawableはDrawableのサブクラスなので、引き続きDrawableをViewに割り当てる必要があります。

BubbleViewは、カスタムクラスのViewではなくImageViewを拡張することをお勧めします。このようにして、メインアクティビティのように、任意のレイアウトでドロアブルを再利用できます。

public class BubbleView extends ImageView { 

    public BubbleView(Context context) {   
     super(context); 
     init(context); 
    } 

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

    public BubbleView(Context context, AttributeSet attrs, int defStyle) {   
     super(context, attrs, defStyle); 
     init(context); 
    } 

    private void init(Context context){ 
     Resources res = this.getResources(); 
     setImageDrawable(res.getDrawable(R.drawable.bubble)); 

     /* Any other initialization */ 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     /* Any other drawing functionality */ 

    } 
} 

役立ちますか?

0
protected void onDraw(Canvas canvas) 
{ 
    super.onDraw(canvas); 

    paint = new Paint(); 

    paint.setColor(Color.WHITE); 


    Rect bounds = new Rect(); 
    paint.getTextBounds(name, 0, name.length(), bounds); 

    LayerDrawable layerDrawable = (LayerDrawable) resources.getDrawable(R.drawable.your_drawble); 
    Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 
    layerDrawable.setBounds(0, 0, getWidth(), getHeight()); 
    layerDrawable.draw(new Canvas(b)); 

    canvas.drawBitmap(b,0,0,paint);} 
関連する問題