Android打造带透明圆弧的ImageView(3)

/**
    * Obtain custom attributes that been defined in attrs.xml.
    * @param attrs A collection of attributes.
    */
    private void obtainAttributes(AttributeSet attrs) {
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.ArcImageView);
        mDrawStr = ta.getString(R.styleable.ArcImageView_drawStr);
        mTextSize = ta.getDimension(R.styleable.ArcImageView_textSize, DEFAULT_TEXT_SIZE);
        mArcAlpha = ta.getInteger(R.styleable.ArcImageView_arcAlpha, DEFAULT_ARC_ALPHA);
        mArcWidth = ta.getInteger(R.styleable.ArcImageView_arcWidth, DEFAULT_ARC_WIDTH);
        mStartAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_START_ANGLE);
        mSweepAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_SWEEP_ANGLE);
        mHOffset = ta.getInteger(R.styleable.ArcImageView_hOffset, DEFAULT_H_OFFSET);
        mVOffset = ta.getInteger(R.styleable.ArcImageView_vOffset, DEFAULT_V_OFFSET);
        mArcColor = ta.getColor(R.styleable.ArcImageView_arcColor, 0XCCCCCC);
        mTextColor = ta.getColor(R.styleable.ArcImageView_textColor, 0XFFFFFF);
        mDrawStyle = ta.getInt(R.styleable.ArcImageView_drawStyle, 0);
        ta.recycle();
    }
}

2.在values文件夹下的attrs.xml中自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable>
        <attr format="string" />
        <attr format="dimension" />
        <attr format="integer" />
        <attr format="integer" />
        <attr format="integer" />
        <attr format="integer" />
        <attr format="float" />
        <attr format="float" />
        <attr format="float" />
        <attr format="enum">
            <enum value="0" />
            <enum value="1" />
            <enum value="2" />
            <enum value="3" />
            <enum value="4" />
        </attr>
        <attr format="color" />
        <attr format="color" />
    </declare-styleable>
</resources>

3.在MainActivity调用ArcImageView,实现代码如下:

package com.linuxidc.customviewsdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.linuxidc.customviewsdemo.views.ArcImageView.ArcImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ArcImageView aiv_one;
    private ArcImageView aiv_two;
    private ArcImageView aiv_three;
    private ArcImageView aiv_four;
    private Button btn_another_one;
    private int mGroup = 1;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aiv_one = (ArcImageView) findViewById(R.id.aiv_one);
        aiv_one.setArcAlpha(180);
        aiv_two = (ArcImageView) findViewById(R.id.aiv_two);
        aiv_two.setArcAlpha(180);
        aiv_three = (ArcImageView) findViewById(R.id.aiv_three);
        aiv_three.setArcAlpha(180);
        aiv_four = (ArcImageView) findViewById(R.id.aiv_four);
        aiv_four.setArcAlpha(180);
        btn_another_one = (Button) findViewById(R.id.btn_another_one);
        btn_another_one.setOnClickListener(this);
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/57c780ddaf74235e22fc733cd8e9577b.html