android小記之自訂ImageView

來源:互聯網
上載者:User

最近,BOSS拍板了,要做個拍照以後的圖片FTP上傳功能。
 
BOSS發話了,就可以動手唄。
 
按照用HTML結合CSS的話,有點審美的,就可以製作漂亮的相框了。
 
但我突然發現android的ImageView的屬性太不給力了,別說漂亮的相框,連加個邊框都難。
 
還好,android具有靈活多變的編碼風格,可以自訂ImageView。為了簡潔,下面的例子只是在ImageView上加了個邊框而已,望啟到拋磚引玉的效果。
 
首先,重寫ImageView。
[java]
package org.xxx.picUpload.util; 
 
 
 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.ImageView; 
 
public class MyImageView extends ImageView { 
     
    public MyImageView(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub 
    } 
 
    public MyImageView(Context context, AttributeSet attrs, 
            int defStyle) { 
        super(context, attrs, defStyle); 
        // TODO Auto-generated constructor stub 
    } 
 
    public MyImageView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub 
    } 
     
    @Override 
    protected void onDraw(Canvas canvas) {   //這裡就是重寫的方法了,想畫什麼形狀自己動手 
        // TODO Auto-generated method stub 
        super.onDraw(canvas); 
         
        // 畫邊框 
        Rect rec = canvas.getClipBounds(); 
        rec.bottom--; 
        rec.right--; 
        Paint paint = new Paint(); 
        paint.setColor(Color.GRAY);   //顏色 
        paint.setStyle(Paint.Style.STROKE); 
        paint.setStrokeWidth(5); 
        canvas.drawRect(rec, paint); 
    } 
     

 
 

然後 在xml檔案中直接引用。
[html]
<org.xxx.picUpload.util.MyImageView 
        android:id="@+id/imageView"     
        android:layout_width="400px" 
        android:layout_height="200px"/> 

有圖有真相(圖片預覽): 


 



摘自 Scott 的專欄
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.