2025-04-23

   一值有在用網上朋友貼出來的代碼,很方便,也能學到很多東西.  這次我也貼點比較簡單使用的小東西. 不怎麼形容  有圖有代碼  自己看真想…
 
 主體代碼
 
Myview代碼 
package com.suncco.taoxie; 
 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 
 
public class MyView extends View { 
    private int count; 
    private float space, radii; 
    private int point_normal_color, point_seleted_color; 
 
    // 選中 
    private int selected = 0; 
 
    // background seleted normal 
 
    public MyView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        TypedArray a = context 
                .obtainStyledAttributes(attrs, R.styleable.MyView); 
 
        count = a.getInteger(R.styleable.MyView_count, 3); 
        space = a.getDimension(R.styleable.MyView_space, 9); 
        radii = a.getDimension(R.styleable.MyView_point_radii, 9); 
 
        point_normal_color = a.getColor(R.styleable.MyView_point_normal_color, 
                0x000000); 
        point_seleted_color = a.getColor( 
                R.styleable.MyView_point_seleted_color, 0xffff07); 
 
        int sum = attrs.getAttributeCount(); 
        a.recycle(); 
    } 
 
    public void setCount(int count) { 
        this.count = count; 
        invalidate(); 
    } 
 
    public void next() { 
        if (selected < count – 1) 
            selected++; 
        else 
            selected = 0; 
        invalidate(); 
    } 
 
    public void previous() { 
        if (selected > 0) 
            selected–; 
        else 
            selected = count – 1; 
        invalidate(); 
    } 
 
    @Override 
    protected void onDraw(Canvas canvas) { 
        Paint paint = new Paint(); 
        paint.setAntiAlias(true); 
        // 起始位置,實現整體居中 
        float w = canvas.getWidth() – (count * 2 * radii) – space * (count – 1); 
 
        for (int i = 0; i < count; i++) { 
            if (i == selected) 
                paint.setColor(point_seleted_color); 
            else 
                paint.setColor(point_normal_color); 
            canvas.drawCircle(w / 2.f + radii + i * (space + radii + radii), 
                    radii + 1, ((int) radii + 2) / 2, paint); 
        } 
    } 
 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
        setMeasuredDimension(widthMeasureSpec, (int) (radii * 2) + 2); 
    } 
 
    public void setSelected(int selectedId) { 
        if (selectedId >= 0 && selectedId <= count) 
            this.selected = selectedId; 
        else if (selectedId < 0) 
            this.selected = 0; 
        else if (selectedId > count) 
            this.selected = count; 
        invalidate(); 
    } 
 

 
 自定義View 的自定義命名空間:
 以下自定義屬性  count: 總數, space: 每個點的距離大小之類的等等 名字取得都比較通俗瞭…
 
命名空間也直接叫myview瞭…代碼 
這個文件xml放在 value 下的  ***_attrs.xml  比如我這裡去  haowuliaoa_attrs.xml 
命名空間也直接叫myview瞭…代碼 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
        <attr name="count" format="integer" />   
        <attr name="space" format="dimension" /> 
        <attr name="point_size" format="dimension" /> 
        <attr name="point_seleted_color" format="color|reference" /> 
        <attr name="point_normal_color" format="color|reference" /> 
        <attr name="point_radii" format="dimension" /> 
    </declare-styleable> 
</resources> 
 
 
一切ok瞭, 可以直接在xml佈局上應用咯…
 
 
Xml代碼 
首先要在佈局的頭 添加上自己的命名空間 
xmlns:haowuliaoa="http://schemas.android.com/apk/res/自己的包名" 
Xml代碼 
然後就是xml佈局咯… 
Xml代碼 
<包名.MyView android:id="@+id/myView" 
                android:layout_width="fill_parent" android:layout_height="10dip" 
                android:background="#00000000" android:gravity="center" 
                android:layout_marginBottom="4dip" suncco:count="6" suncco:space="10dip" 
                suncco:point_size="4dip" suncco:point_seleted_color="#ff0000" 
                suncco:point_normal_color="#ffffff" suncco:point_radii="5dip" /> 
 
so 簡單吧….
圖片下面的小點就是今天的效果哦 很常用的
功能卻是經常能用到的哦  …

下面的小點就是今天要的效果哦, 很常用的吧.

作者“whyhappy”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *