Android學習筆記之給圖片加邊框

openkk 12年前發布 | 59K 次閱讀 Android Android開發 移動開發

Android學習筆記進階19之給圖片加邊框

//設置顏色
    public void setColour(int color){
        co = color;
    }
    //設置邊框寬度
    public void setBorderWidth(int width){

        borderwidth = width;
    }
具體實現:
package xiaosi.imageborder;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class ImageBorderActivity extends Activity {
    /** Called when the activity is first created. */
    private myImageView image = null;
    private myImageView image1 = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image = (myImageView)findViewById(R.id.iamge);
        image.setColour(Color.YELLOW);
        image.setBorderWidth(10);
        image1 = (myImageView)findViewById(R.id.iamge1);
        image1.setColour(Color.GREEN);
        image1.setBorderWidth(5);
    }
}
main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/playerbackground"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <xiaosi.imageborder.myImageView 
         android:id="@+id/iamge"
         android:layout_width="200px" 
         android:layout_height="230px"
         android:layout_alignParentRight="true"
         android:src="@drawable/v"
         android:layout_centerInParent="true"
         android:layout_marginRight="3px"
        />
     <xiaosi.imageborder.myImageView 
         android:id="@+id/iamge1"
         android:layout_width="200px" 
         android:layout_height="230px"
         android:layout_alignParentRight="true"
         android:src="@drawable/v"
         android:layout_centerInParent="true"
         android:layout_marginRight="3px"
        />
</LinearLayout>
package xiaosi.imageborder;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

public class myImageView extends ImageView {

    private int co;
    private int borderwidth;
    public myImageView(Context context) {
        super(context);
    }
    public myImageView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public myImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //設置顏色
    public void setColour(int color){
        co = color;
    }
    //設置邊框寬度
    public void setBorderWidth(int width){

        borderwidth = width;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 畫邊框
        Rect rec = canvas.getClipBounds();
        rec.bottom--;
        rec.right--;
        Paint paint = new Paint();
        //設置邊框顏色
        paint.setColor(co);
        paint.setStyle(Paint.Style.STROKE);
        //設置邊框寬度
        paint.setStrokeWidth(borderwidth);
        canvas.drawRect(rec, paint);
    }
}

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!