關於android自訂view的方法

來源:互聯網
上載者:User

關於android自訂view的方法
1.自訂view從哪裡開始呢!從哪裡開始畫?View有幾個方法。
其中一個大家都知曉的OnDraw(), 但是,如果想要隨時調用隨時重新整理的的話,用什麼呢?如果做過win api開發的話,就會知道inVidate()方法。這個是通知開始重繪的方法,執行了invaidate之後就會重新開始ondraw()。
2.如果需要添加自訂屬性。

  對於自訂的View如果沒有自己獨特的屬性,可以直接在xml檔案中使用就可以了  如果含有自己獨特的屬性,那麼就需要在建構函式中擷取屬性檔案attrs.xml中自訂屬性的名稱  並根據需要設定預設值,放在在xml檔案中沒有定義。  如果使用自訂屬性,那麼在應用xml檔案中需要加上新的schemas,  比如這裡是xmlns:my="http://schemas.android.com/apk/res/包名"  其中xmlns後的“my”是自訂的屬性的首碼,res後的是我們應用所在的包

自訂屬性的用法如下:
第一步:attrs.xml添加一段你自訂的屬性名稱,還有格式:
  
  
        
    

第二步:xmlns:tri="http://schemas.android.com/apk/res/包名"  這句話需要加在最外層的layout屬性中,或者加在view的屬性裡。

第三步:寫進來添加的自訂屬性
        />

第四步:在onDraw方法裡,調用:
可以再context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
我這裡重新提出了一個方法:
 private void getAttr(Context context, AttributeSet attrs)
    {
        array = context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
        maincolor = array.getColor(R.styleable.TriangleViewAttr_tricolor, maincolor); 
        //TriangleViewAttr_tricolor 就是TriangleViewAttr.tricolor
        array.recycle();
    }
 有一句:array.recycle();很重要,記得回收哦!

既然,已經獲得指定的屬性值。
那麼,第五步:
根據你所獲得的自訂屬性值,進行如下操作吧!


OK!那麼,我把My Code貼上來吧!
1.首先是attr.xml:
xml version="1.0" encoding="utf-8"?>
<resources>




<declare-styleable name = "TriangleViewAttr">
<attr name = "tricolor" format = "color" />
declare-styleable>

resources>

2.TriangleView類的代碼:

package com.commons.widget.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.jsdx.zqysypt.R;

/**
* Created by aa on 2015/1/12.
* 畫一個三角形 三個點 1.最左上方 2.最左下角 3.右邊的中間點; :
* @*****
* *****@
* @*****
*
* 用法
* xmlns:tri="http://schemas.android.com/apk/res/包名"
* android:layout_width="12dp"
* android:layout_height="12dp"
* tri:tricolor="@color/ios_blue" />
*/
public class TriangleView extends View {
//預設顏色是透明的色彩
int maincolor=Color.TRANSPARENT;
//是否為等邊三角
boolean isEquilateral=false;


//tri:tricolor="@color/ios_blue"
//TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的數組
//在使用完成後,一定要調用recycle方法
//屬性的名稱是styleable中的名稱+“_”+屬性名稱
TypedArray array = null;

public TriangleView(Context context) {
super(context);
}

public TriangleView(Context context, AttributeSet attrs) {
super(context, attrs);
getAttr(context, attrs);
}

public TriangleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getAttr(context, attrs);
}

private void getAttr(Context context, AttributeSet attrs)
{
array = context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
maincolor = array.getColor(R.styleable.TriangleViewAttr_tricolor, maincolor); //TriangleViewAttr_tricolor 就是TriangleViewAttr.tricolor
array.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 建立畫筆
Paint p = new Paint();
p.setColor(maincolor);// 設定紅色
p.setStyle(Paint.Style.FILL);//設定填滿

//獲得幾個點的數值
int width=getWidth();
int height=getHeight();
int loc_x=getLeft();
int loc_y=getTop();
Log.d("TriangleView",width+" "+height);


// 繪製這個三角形,你可以繪製手繪多邊形
Path path = new Path();
path.moveTo(0, 0);// 此點為多邊形的起點

if(isEquilateral)
path.lineTo((height/2)*1.73205f, height/2); ///這裡使用*1.73205f 是因為如果要畫一個等邊三角形的話需要用這個比例根號三一條直角邊是另外一條直角邊的 1.73205f 倍。
else
path.lineTo(width, height/2);

path.lineTo(0, height);
path.close(); // 使這些點構成封閉的多邊形
canvas.drawPath(path, p);

}


/**重新設定顏色
* @param color
* (0xe96f4a)無效 (0xffe96f4a)這樣就可以了
*/
public void showColor(int color)
{
maincolor=color;
this.invalidate();
}

}
3.layout布局xml:

<com.commons.widget.views.TriangleView
xmlns:triattr="http://schemas.android.com/apk/res/com.jsdx.zqysypt"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
triattr:tricolor="@color/mainact_lefttopblue" />














聯繫我們

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