zoukankan      html  css  js  c++  java
  • Android学习笔记进阶19 之给图片加边框

    1. //设置颜色  
    2.     public void setColour(int color){  
    3.         co = color;  
    4.     }  
    5.     //设置边框宽度  
    6.     public void setBorderWidth(int width){  
    7.           
    8.         borderwidth = width;  
    9.     }  


     

    具体实现:

    1. package xiaosi.imageborder;  
    2.   
    3. import android.app.Activity;  
    4. import android.graphics.Color;  
    5. import android.os.Bundle;  
    6.   
    7. public class ImageBorderActivity extends Activity {  
    8.     /** Called when the activity is first created. */  
    9.     private myImageView image = null;  
    10.     private myImageView image1 = null;  
    11.     @Override  
    12.     public void onCreate(Bundle savedInstanceState) {  
    13.         super.onCreate(savedInstanceState);  
    14.         setContentView(R.layout.main);  
    15.           
    16.         image = (myImageView)findViewById(R.id.iamge);  
    17.         image.setColour(Color.YELLOW);  
    18.         image.setBorderWidth(10);  
    19.         image1 = (myImageView)findViewById(R.id.iamge1);  
    20.         image1.setColour(Color.GREEN);  
    21.         image1.setBorderWidth(5);  
    22.     }  
    23. }  


     

    main.xml

    1. <LinearLayout  
    2.     xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:background="@drawable/playerbackground"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent">  
    6.      <xiaosi.imageborder.myImageView   
    7.          android:id="@+id/iamge"  
    8.          android:layout_width="200px"   
    9.          android:layout_height="230px"  
    10.          android:layout_alignParentRight="true"  
    11.          android:src="@drawable/v"  
    12.          android:layout_centerInParent="true"  
    13.          android:layout_marginRight="3px"  
    14.         />  
    15.      <xiaosi.imageborder.myImageView   
    16.          android:id="@+id/iamge1"  
    17.          android:layout_width="200px"   
    18.          android:layout_height="230px"  
    19.          android:layout_alignParentRight="true"  
    20.          android:src="@drawable/v"  
    21.          android:layout_centerInParent="true"  
    22.          android:layout_marginRight="3px"  
    23.         />  
    24. </LinearLayout>  


     

    1. package xiaosi.imageborder;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Canvas;  
    5. import android.graphics.Paint;  
    6. import android.graphics.Rect;  
    7. import android.util.AttributeSet;  
    8. import android.widget.ImageView;  
    9.   
    10. public class myImageView extends ImageView {  
    11.   
    12.     private int co;  
    13.     private int borderwidth;  
    14.     public myImageView(Context context) {  
    15.         super(context);  
    16.     }  
    17.     public myImageView(Context context, AttributeSet attrs,  
    18.             int defStyle) {  
    19.         super(context, attrs, defStyle);  
    20.     }  
    21.   
    22.     public myImageView(Context context, AttributeSet attrs) {  
    23.         super(context, attrs);  
    24.     }  
    25.     //设置颜色  
    26.     public void setColour(int color){  
    27.         co = color;  
    28.     }  
    29.     //设置边框宽度  
    30.     public void setBorderWidth(int width){  
    31.           
    32.         borderwidth = width;  
    33.     }  
    34.     @Override  
    35.     protected void onDraw(Canvas canvas) {  
    36.         super.onDraw(canvas);  
    37.         // 画边框  
    38.         Rect rec = canvas.getClipBounds();  
    39.         rec.bottom--;  
    40.         rec.right--;  
    41.         Paint paint = new Paint();  
    42.         //设置边框颜色  
    43.         paint.setColor(co);  
    44.         paint.setStyle(Paint.Style.STROKE);  
    45.         //设置边框宽度  
    46.         paint.setStrokeWidth(borderwidth);  
    47.         canvas.drawRect(rec, paint);  
    48.     }  
    49. }  


     

    源代码下载:点击打开链接

  • 相关阅读:
    [转载]PostgreSQL 的昨天、今天和明天 Joe
    [转载]ArcObjects使用小记~Singleton objects Joe
    [小记]postgresql的系统表 Joe
    ~PostgreSQL About~ Joe
    [转]Three things you should never put in your database Joe
    经典问题之汉诺塔
    经典问题之费式级数
    ExtJs 表单和表格之间进行数据交互
    commonfileupload 上传单个或者多个文件 示例
    软件工程中10个重要思想
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722561.html
Copyright © 2011-2022 走看看