【第22期】观点:IT 行业加班,到底有没有价值?

Android中自定义属性(attr.xml,TypedArray)+Gallery

原创 2012年03月26日 23:01:43

部分关键代码

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.wo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/homeGallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="10dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="奇异UIdemo"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <com.wo.FlowIndicator
            android:id="@+id/myView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            app:count="4"
            android:gravity="center"
            app:point_normal_color="#45000000"
            app:point_radius="3dip"
            app:point_selected_color="#ffffff"
            app:space="10dip" >
        </com.wo.FlowIndicator>
    </LinearLayout>

</FrameLayout>


FlowIndicator.java

package com.wo;

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 FlowIndicator extends View {
    private int count;
    private float space, radius;
    private int point_normal_color, point_seleted_color;

    // 选中
    private int seleted = 0;

    // background seleted normal

    public FlowIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.FlowIndicator);

        count = a.getInteger(R.styleable.FlowIndicator_count, 4);
        space = a.getDimension(R.styleable.FlowIndicator_space, 9);

        radius = a.getDimension(R.styleable.FlowIndicator_point_radius, 9);

        point_normal_color = a.getColor(
                R.styleable.FlowIndicator_point_normal_color, 0x000000);
        point_seleted_color = a.getColor(
                R.styleable.FlowIndicator_point_selected_color, 0xffff07);

        int sum = attrs.getAttributeCount();
        // if (Constans.DEBUG) {
        String str = "";
        for (int i = 0; i < sum; i++) {
            String name = attrs.getAttributeName(i);
            String value = attrs.getAttributeValue(i);
            str += "attr_name :" + name + ": " + value + "\n";
        }
        Log.i("attribute", str);
        // }
        a.recycle();

    }

    public void setSeletion(int index) {
        this.seleted = index;
        invalidate();
    }

    public void setCount(int count) {
        this.count = count;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        float width = (getWidth() - ((radius * 2 * count) + (space * (count - 1)))) / 2.f;

        for (int i = 0; i < count; i++) {
            if (i == seleted)
                paint.setColor(point_seleted_color);
            else
                paint.setColor(point_normal_color);

            canvas.drawCircle(width + getPaddingLeft() + radius + i
                    * (space + radius + radius), getHeight() / 2, radius, paint);

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) (getPaddingLeft() + getPaddingRight()
                    + (count * 2 * radius) + (count - 1) * space + 1);
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) (2 * radius);
        }
        return result;
    }

}

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="FlowIndicator">
        <attr format="integer" name="count" />
        <attr format="dimension" name="space" />
        <attr format="dimension" name="point_size" />
        <attr format="color|reference" name="point_selected_color" />
        <attr format="color|reference" name="point_normal_color" />
        <attr format="dimension" name="point_radius" />
    </declare-styleable>

</resources>





版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用

 以launcher为例说明自定义控件的属性: 1、  在layout里面定义控件,如:      xmlns:android="http://schemas.android.com/...

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击Android高手进阶教程(三)<

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:a...

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用(转)

一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:     &lt;?xml version="1.0" encoding="utf...

Android之 declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用

[b]一、在res/values文件下定义一个attrs.xml文件,代码如下[/b]: [code="java"]
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)