自定义视图属性&&自定义控件皮肤&&使用绘图API自定义视图

1、自定义视图属性

  • MyRectV.java
  •  1 public class MyRectV extends View {
     2 
     3     //有资源解析程序使用,由资源解析器房访问
     4     public MyRectV(Context context, AttributeSet attrs) {
     5         super(context, attrs);
     6         
     7         //与attrs.xml文件中的MyRectView_style进行绑定
     8         TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MyRectView_style);
     9         //Attribute color value, or defValue if not defined.
    10         int color = tArray.getColor(R.styleable.MyRectView_style_myrect_color, 0xff000000);
    11         
    12         setBackgroundColor(color);
    13         tArray.recycle();
    14     }
    15     
    16     public MyRectV(Context context) {
    17         super(context);
    18     }
    19 
    20 }

     

  • 新建存于values文件中attrs.xml
  • 1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <declare-styleable name="MyRectView_style">
    4         <attr name = "myrect_color" format="color"/>
    5     </declare-styleable>
    6 </resources>

     

  • activity_main.xml
  •  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     
     3     xmlns:liyang ="http://schemas.android.com/apk/res/com.example.myrectview"
     4     
     5     xmlns:tools="http://schemas.android.com/tools"
     6     android:layout_width="match_parent"
     7     android:layout_height="match_parent"
     8     android:orientation="vertical"
     9     tools:context="com.example.myrectview.MainActivity" >
    10 
    11     <com.example.myrectview.MyRectV 
    12         android:layout_width = "100dp"
    13         android:layout_height = "100dp" 
    14         liyang:myrect_color = "#ff00ff00"/>
    15 
    16 </LinearLayout>

     

  • 注意:ecllipse显示可能和实际中的不一样。

2、自定义控件皮肤:

  • activity_main.xml
  •  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingTop="@dimen/activity_vertical_margin"
     6     tools:context="com.example.mybutton.MainActivity" >
     7 
     8     <Button
     9         android:id="@+id/button1"
    10         android:background="@drawable/button_skin"
    11         android:layout_width="wrap_content"
    12         android:layout_height="wrap_content"
    13         android:text="Button" />
    14 
    15 </LinearLayout>

     

  • button_skin.xml
  • 1 <?xml version="1.0" encoding="utf-8"?>
    2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    3     <item android:state_pressed="false" android:drawable="@drawable/imag1"></item>
    4     <item android:state_pressed="true" android:drawable="@drawable/imag2"></item>
    5 </selector>

     

3、使用绘图API自定义视图:

  • MyRect.java
  •  1 public class MyRect extends View {
     2     
     3     private Paint paint;
     4 
     5     private float degrees;
     6     
     7     private int d_offset;
     8     
     9     public MyRect(Context context, AttributeSet attrs, int defStyleAttr) {
    10         super(context, attrs, defStyleAttr);
    11         Init();
    12     }
    13 
    14     public MyRect(Context context, AttributeSet attrs) {
    15         super(context, attrs);
    16         Init();
    17     }
    18 
    19     public MyRect(Context context) {
    20         super(context);
    21         Init();
    22     }
    23 
    24     private void Init() {
    25         d_offset= 0;
    26         degrees = 0;
    27         paint = new Paint();
    28         paint.setColor(Color.BLACK);
    29     }
    30     
    31     @Override
    32     public void draw(Canvas canvas) {
    33         super.draw(canvas);
    34         
    35         canvas.save();
    36         canvas.translate(d_offset, d_offset);
    37         canvas.rotate(degrees,d_offset+50,d_offset+50);
    38         canvas.drawRect(0, 0,100,100,paint);
    39         
    40         d_offset++;
    41         degrees++;
    42         
    43         canvas.restore();
    44         
    45         invalidate();
    46         
    47     }
    48 }

     

  • activaty_main.xml
  •  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.mydrawnrect.MainActivity" >
    10 
    11     <com.example.mydrawnrect.MyRect 
    12         android:layout_width= "fill_parent"
    13         android:layout_height="fill_parent"
    14         />
    15     
    16 
    17 </RelativeLayout>

     

posted @ 2015-05-29 20:23  何人之名  阅读(137)  评论(0)    收藏  举报