【问题标题】:Problem with custom View using LayerDrawable not displaying anything使用 LayerDrawable 的自定义视图问题不显示任何内容
【发布时间】:2011-08-01 01:46:53
【问题描述】:

我在扩展 View 类时遇到问题,XML 可以正常工作,因为我在 ImageView 上尝试过,但在我的自定义类中没有...

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(在 res/drawable-mdpi 文件夹中)

<?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);
  }
}

谢谢!

【问题讨论】:

    标签: android view drawable layer


    【解决方案1】:

    LayerDrawable 是 Drawable 的子类,因此您仍然需要将您的 drawable 分配给 View。

    我建议 BubbleView 为您的自定义类扩展 ImageView 而不是 View。这样你就可以在任何你想要的布局中重复使用你的drawable,就像在你的主要活动中一样。

    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 */
    
        }
    }
    

    有帮助吗?

    【讨论】:

      【解决方案2】:
      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);}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多