自定义控件+二维码

来源:互联网 发布:安卓博洋软件 编辑:程序博客网 时间:2024/05/30 07:14

1.title.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:background="#D4DBED"    >    <TextView        android:textSize="20sp"        android:gravity="center"        android:id="@+id/fh"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回"        android:layout_weight="1"        />   <TextView       android:textSize="25sp"       android:gravity="center"       android:id="@+id/titletv"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="那些花儿"       android:layout_weight="2"       />    <TextView        android:textSize="20sp"        android:gravity="center"        android:id="@+id/tz"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转"        android:layout_weight="1"        /></LinearLayout>

2.自定义标题dytitle类

public class dytitle extends LinearLayout implements View.OnClickListener {    private TextView fh;    private TextView tz;    private TextView titletv;    private Return_Interface return_interface;    public void setDytitle(Return_Interface return_interface){        this.return_interface = return_interface;    }    public dytitle(Context context) {        this(context,null);    }    public dytitle(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public dytitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        View view = View.inflate(context, R.layout.title, this);        fh = (TextView) view.findViewById(R.id.fh);        tz = (TextView) view.findViewById(R.id.tz);        titletv = (TextView) view.findViewById(R.id.titletv);        fh.setOnClickListener(this);        tz.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId())        {            case R.id.fh:                //return_interface.returnOnClick();              // Intent intent2 = new Intent(getContext(), MainActivity.class);               // getContext().startActivity(intent2);                //view.getcontext().finish();.                break;            case R.id.tz:                Intent intent = new Intent(getContext(), Main2Activity.class);                getContext().startActivity(intent);                break;        }    }}

3.自定义圆环dyyh类

public class dyyh  extends View implements  Runnable  {   /* Handler  handler =new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);         // Intent intent = new Intent(getContext(), CaptureActivity.class);            //startActivityForResult(intent,1);           }    };*/    Paint mpaint = new Paint();    Paint mtvpaint = new Paint();    Paint Mpaint = new Paint();    private int progress=0;    private RectF rectF;    private boolean b = true;    private  int  banjing=100;    private Return_Interface return_interface;    public void setReturn_interface(Return_Interface return_interface) {        this.return_interface = return_interface;    }    public dyyh(Context context) {        this(context,null);    }    public dyyh(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public dyyh(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.dyyh, defStyleAttr, 0);         banjing = typedArray.getInt(R.styleable.dyyh_dyyhradio,defStyleAttr);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        int centerx=w/2;        int centery=h/2;        rectF = new RectF();        rectF.left=centerx-banjing;        rectF.top=centery-banjing;        rectF.right=centerx+banjing;        rectF.bottom=centery+banjing;    }    private void init() {        mpaint.setColor(Color.parseColor("#00FE00"));        mpaint.setStyle(Paint.Style.STROKE);        mpaint.setStrokeWidth(20);        Mpaint.setColor(Color.parseColor("#888888"));        Mpaint.setStyle(Paint.Style.STROKE);        Mpaint.setStrokeWidth(20);        mtvpaint.setTextSize(25);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        huahuan(canvas);        huatext(canvas);    }    private void huatext(Canvas canvas) {        String  tv=progress+"%";        Paint.FontMetrics fm = new Paint.FontMetrics();        //得到文字的高度        int tvh= (int) Math.ceil(fm.descent-fm.ascent);        int tvw= (int) mtvpaint.measureText(tv,0,tv.length());        canvas.drawText(tv,getWidth()/2-tvw/2,getHeight()/2-tvh/2,mtvpaint);    }    private void huahuan(Canvas canvas) {        canvas.drawArc(rectF,-90,360,false,Mpaint);        canvas.drawArc(rectF,-90,(360*progress)/100,false,mpaint);    }    @Override    public void run() {        while(true)        {            if(progress<100)            {                progress++;                try {                    Thread.sleep(100);                    postInvalidate();                } catch (Exception e) {                    e.printStackTrace();                }                b = false;             }else{                b = true;                progress=0;                return_interface.returnOnClick(b);                break;            }             /*if(progress>=100)             {               try {                     Thread.sleep(2000);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }                 Intent intent = new Intent(getContext(), Main2Activity.class);                 getContext().startActivity(intent);            }*/        }    }   /* public void tz(final Return_Interface return_interface){        return_interface.returnOnClick();        return;    }*/}

4.自定义流式布局lsbj类

public class lsbj  extends ViewGroup{    public lsbj(Context context) {        this(context,null);    }    public lsbj(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public lsbj(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //测量view        measureChildren(widthMeasureSpec,heightMeasureSpec);    }    /**     * 用来确定子View的位置     * @param changed     * @param l  lef 相对于父容器     * @param t  top     * @param r  right     * @param b  bottom     */    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {         int  currentwidth=0;         int  currentHeight=0;        //得到子View的个数        int  childcount=getChildCount();        for(int i=0;i<childcount;i++)        {            //得到这个viewgroup里面的每一个子view            View childView=getChildAt(i);            //得到每一个子view的宽高            int width=childView.getMeasuredWidth();            int height=childView.getMeasuredHeight();            //确定子view 的高度           childView.layout(currentwidth,currentHeight,currentwidth+width,currentHeight+height);            //累加所有的字view宽高            currentwidth+=width;            currentHeight+=height;        }}}

5.自定义的接口Return_Interface

public interface Return_Interface {        void returnOnClick(boolean b);}

6.MainActivity

public class MainActivity extends AppCompatActivity{    private dytitle dyt;    private dyyh dyh;    private TextView fh;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        dyt = (dytitle) findViewById(R.id.dyt);        dyh = (dyyh) findViewById(R.id.dyh);        fh = (TextView) dyt.findViewById(R.id.fh);        fh.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                finish();            }        });    }    public   void  sm(View v)    {        new Thread(dyh).start();        dyh.setReturn_interface(new Return_Interface() {            @Override            public void returnOnClick(boolean b) {                if(b){                    Intent intent = new Intent(MainActivity.this, CaptureActivity.class);                    startActivityForResult(intent,1);                }            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == 1) {            //处理扫描结果(在界面上显示)            if (null != data) {                Bundle bundle = data.getExtras();                if (bundle == null) {                    return;                }                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {                    String result = bundle.getString(CodeUtils.RESULT_STRING);                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {                    Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();                }            }        }    }}

7.Main2Activity

public class Main2Activity extends AppCompatActivity {    private dytitle dy_title;    private TextView fh;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        dy_title = (dytitle)findViewById(R.id.ddd);        fh = (TextView)dy_title.findViewById(R.id.fh);        fh.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                finish();            }        });    }}

8.activity_main

<com.example.zhoukao1mn.view.dytitle       android:id="@+id/dyt"       android:layout_width="match_parent"       android:layout_height="wrap_content">   </com.example.zhoukao1mn.view.dytitle>   <com.example.zhoukao1mn.view.dyyh       aaa:dyyhradio="100"       android:id="@+id/dyh"       android:layout_width="match_parent"       android:layout_height="261dp" /><Button    android:id="@+id/sm"    android:onClick="sm"    android:text="扫描二维码"    android:layout_width="match_parent"    android:layout_height="wrap_content" />

9.activity_main2

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.zhoukao1mn.Main2Activity"    android:orientation="vertical"    >    <com.example.zhoukao1mn.view.dytitle        android:id="@+id/ddd"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </com.example.zhoukao1mn.view.dytitle>    <com.example.zhoukao1mn.view.lsbj        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="150dp"            android:layout_height="wrap_content"            android:textSize="25sp"            android:padding="5dp"            android:background="#619EA0"            />        <TextView            android:layout_width="150dp"            android:layout_height="wrap_content"            android:textSize="25sp"            android:padding="5dp"            android:background="#7561A0"            />        <TextView            android:layout_width="150dp"            android:layout_height="wrap_content"            android:textSize="25sp"            android:padding="5dp"            android:background="#9FA061"            /></com.example.zhoukao1mn.view.lsbj></LinearLayout>

10.二维码的依赖,权限app类见下一篇博客

这里写图片描述

原创粉丝点击