android 層疊圖片形成一張圖片

來源:互聯網
上載者:User

代碼實現:

private ImageView mImageView;

    private TextView tView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mImageView=(ImageView)findViewById(R.id.image);
        tView=(TextView)findViewById(R.id.tv);
        setImage();
    }
    
    
    public void setImage(){
        Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon);    
        Drawable[] array = new Drawable[3];    
         array[0] = new PaintDrawable(Color.BLUE); //黑色    
         array[1] = new PaintDrawable(Color.WHITE); //白色       
         array[2] = new BitmapDrawable(bm); //位元影像資源            
        LayerDrawable ld = new LayerDrawable(array); //參數為上面的Drawable數組    
        ld.setLayerInset(0, 1, 1, 1, 1);  //第一個參數1代表數組的第二個元素,為白色    
        ld.setLayerInset(1, 2, 2, 2, 2);  //第一個參數1代表數組的第二個元素,為白色    
        ld.setLayerInset(2, 3, 3, 3, 3); //第一個參數2代表數組的第三個元素,為位元影像資源  
//        mImageView.setBackgroundResource(R.drawable.icon);
        mImageView.setImageDrawable(ld);     

    }

資源檔實現:

首先建立一個drawalbe/login_head.xml的檔案,內容如下:

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <layer-list  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item android:id="@+id/user_faceback_drawable" android:drawable="@drawable/faceback" />  
  5.     <item android:id="@+id/user_face_drawable" android:drawable="@drawable/h001" android:left="10.0dip" android:top="18.0dip" android:right="25.0dip" android:bottom="35.0dip" />  
  6. </layer-list>  

然後就可以使用這個組合的圖片了,比如在ImageView中顯示:
<ImageView android:id="@+id/faceImg"
android:background="@drawable/login_head"

android:layout_width="wrap_content"
android:layout_height="wrap_content" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.