画圆



<?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.zhoukaomoni1.Main2Activity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:id="@+id/iv_back_too"
            android:layout_marginLeft="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="那些花儿"
            android:id="@+id/tv_flowr"
            android:layout_centerInParent="true"/>
        <ImageView

            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:src="@drawable/myperson"
            android:id="@+id/iv_person_too"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
    <com.example.zhoukaomoni1.TiActivity
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:background="#1180d6"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="111111111"
            />
        <TextView
            android:background="#bc0cb6"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="222222222"
            />
        <TextView
            android:background="#f0690f"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="333333333"
            />
    </com.example.zhoukaomoni1.TiActivity>
</LinearLayout>




//main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhoukaomoni1.MainActivity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:id="@+id/iv_back"
            android:layout_marginLeft="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="那些花儿"
            android:id="@+id/tv_flower"
            android:layout_centerInParent="true"/>
        <ImageView
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:src="@drawable/myperson"
            android:id="@+id/iv_person"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
    <com.example.zhoukaomoni1.ErActivity
        android:layout_gravity="center"
        android:id="@+id/erActivity_hehe"
        android:layout_width="200dp"
        android:layout_height="200dp"
         />
    <Button
        android:id="@+id/but_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始"/>

</LinearLayout>




package com.example.zhoukaomoni1;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private static final int FLAG = 0x123; private ImageView ivPerson; private Button but_start; private ErActivity erActivityHehe; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case FLAG: int progress = erActivityHehe.getProgress(); progress +=20; if (progress > 100) { handler.removeMessages(msg.what); handler.removeMessages(FLAG); progress = 0; } erActivityHehe.setProgress(progress); handler.sendEmptyMessageDelayed(FLAG,1000); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivPerson = (ImageView) findViewById(R.id.iv_person); but_start = (Button) findViewById(R.id.but_start); erActivityHehe = (ErActivity) findViewById(R.id.erActivity_hehe); but_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { handler.sendEmptyMessageDelayed(FLAG,1000); } }); ivPerson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } }); erActivityHehe.setProgressUpdateListener(new ErActivity.ProgressUpdateListener() { @Override public void onProgressUpdated(int progress) { int progress1 = erActivityHehe.getProgress(); if (progress1>=100){ handler.removeMessages(FLAG); } } }); /*mCirclePercentView = (ErActivity) findViewById(R.id.circlePercentView); mCirclePercentView.setOnCircleClickListener(new ErActivity.OnClickListener() { @Override public void onClick(View v) { float percent = (float) (Math.random() * 99 + 1); mCirclePercentView.setCurPercent(percent); } });*/ } @Override protected void onStop() { super.onStop(); int progress1 = erActivityHehe.getProgress(); if (progress1>=100){ handler.removeMessages(FLAG); } }}

package com.example.zhoukaomoni1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author:Created by WangZhiQiang on 2017/11/4.
 */

public class ErActivity extends View{

    private ProgressUpdateListener listener;

    interface ProgressUpdateListener{
        void onProgressUpdated(int progress);
    }
    public void setProgressUpdateListener(ProgressUpdateListener listener){
        this.listener=listener;
    }
    private int progress=10;
    public int getProgress() {
        return progress;
    }
    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
        if (listener!=null){
            listener.onProgressUpdated(progress);
        }
    }

    public ErActivity(Context context) {
        this(context,null);
    }

    public ErActivity(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ErActivity(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int min = Math.min(width, height);
        int ratio = min/2;
        Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1.setColor(Color.GREEN);
        paint1.setStyle(Paint.Style.FILL);
        canvas.drawCircle(ratio,ratio,ratio,paint1);
        //扇形
        paint1.reset();
        paint1.setStyle(Paint.Style.FILL);
        paint1.setStrokeWidth(20);
        paint1.setColor(Color.RED);
        RectF rectF = new RectF();
        rectF.set(0,0, min, min);
        canvas.drawArc(rectF,-90,progress*360/100, true, paint1);
        //小圆
        paint1.reset();
        paint1.setColor(Color.WHITE);
        paint1.setStyle(Paint.Style.FILL);
        canvas.drawCircle(ratio,ratio,ratio-20,paint1);

        //画字
        paint1.reset();
        paint1.setColor(Color.BLUE);
        paint1.setTextSize(30);
        Rect rect=new Rect();
        paint1.getTextBounds(progress+"%",0,String.valueOf(progress+"%").length(),rect);
        int width1 = rect.width();
        int height1 = rect.height();
        canvas.drawText(progress+"%",ratio-width1/2,ratio+height1/2,paint1);


    }
}
//梯形布局
package com.example.zhoukaomoni1;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * author:Created by WangZhiQiang on 2017/11/4.
 */

public class TiActivity extends ViewGroup {
    public TiActivity(Context context) {
        this(context,null);
    }

    public TiActivity(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TiActivity(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int width=0;
        int height=0;
        for(int i =0;i<childCount;i++){
            View v = getChildAt(i);
            v.layout(width,height,width+v.getMeasuredWidth(),height+v.getMeasuredHeight());
            width+=v.getMeasuredWidth();
            height+=v.getMeasuredHeight();
        }

    }
}
//px于dp的转换
package com.example.zhoukaomoni1;

import android.content.Context;

/**
 * author:Created by WangZhiQiang on 2017/11/4.
 */

public class DensityUtils {
/** 
      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
      */
            public static int dip2px(Context context, float dpValue){
        final float scale = context.getResources().getDisplayMetrics().density;
                return (int) (dpValue * scale + 0.5f);
            }
            /** 
          * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
          */
            public static int px2dip(Context context, float pxValue) {
                final float scale = context.getResources().getDisplayMetrics().density;
                return (int) (pxValue / scale + 0.5f);
}
}
//跳转到的页面
package com.example.zhoukaomoni1;

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

public class Main2Activity extends AppCompatActivity {

    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        back = (ImageView) findViewById(R.id.iv_back_too);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}





 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值