如何使用View自定义属性画圆

//主方法
package com.example.tianjiale20160808;

import view.LegePaint;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText ed2;
	private EditText ed3;
	private EditText ed4;
	private Button but5;
	private LegePaint mlege;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findid();

	}

	private void findid() {
		ed2 = (EditText) findViewById(R.id.ed2);
		ed3 = (EditText) findViewById(R.id.ed3);
		ed4 = (EditText) findViewById(R.id.ed4);
		but5 = (Button) findViewById(R.id.but5);
		mlege = (LegePaint) findViewById(R.id.mlegeView);
		Log.i("lege", "aaaaaaaaaaaaaaaaaaaaaaaa");

		but5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String trim1 = ed2.getText().toString().trim();
				String trim2 = ed3.getText().toString().trim();
				String trim3 = ed4.getText().toString().trim();
				if (trim1.length() >= 1 && trim1.length() >= 1
						&& trim1.length() >= 1) {
					//强转String型为Float型       参数不能为空
					float price1 = Float.parseFloat(trim1);
					float price2 = Float.parseFloat(trim2);
					Log.i("lege", price1 + "");
					Log.i("lege", price2 + "");
					Log.i("lege", trim3);
					//通过有参构造 赋值
					mlege.setP1(price1);
					mlege.setP2(price2);
					mlege.setTrim(trim3);
					//从新绘制
					mlege.invalidate();
				} else {
					Toast.makeText(MainActivity.this, "最后一个参数格式是   #FF0000", 0).show();
				}
			}
		});
	}

}

//集成view类
package view;

import com.example.tianjiale20160808.MainActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class LegePaint extends View {
    private Context context;
    private float p1=10;
    private float p2=5;
    private String trim="#ffffff";
    private Paint mpaint;
    

    public float getP1() {
        return p1;
    }

    public void setP1(float p1) {
        this.p1 = p1;
    }

    public float getP2() {
        return p2;
    }

    public void setP2(float p2) {
        this.p2 = p2;
    }

    public String getTrim() {
        return trim;
    }

    public void setTrim(String trim) {
        this.trim = trim;
    }



    public LegePaint(Context context) {
        this(context,null);
    }
    public LegePaint(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public LegePaint(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mpaint =new Paint(Paint.ANTI_ALIAS_FLAG);
        mpaint.setStyle(Paint.Style.STROKE);
        
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mpaint.setStrokeWidth(p2);
        mpaint.setColor(Color.parseColor(trim));
        Log.i("le", p1+"aaaaaaaaaaaaa"+p2+"trim");
        canvas.drawCircle(300, 300, p1, mpaint);
    }
    public void shuxin(){
        invalidate();
    }
}


//布局文件

<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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <LinearLayout 
        android:id="@+id/bu1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00FF00">
        <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义圆环" 
        android:layout_marginLeft="120dp"
        android:layout_marginTop="20dp"
        />
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/bu2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
       >
        <TextView
        android:id="@+id/title2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆的半径" 
        />
        <EditText 
            android:id="@+id/ed2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="9"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/bu3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
       >
        <TextView
        android:id="@+id/title3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆环的宽度" 
        />
        <EditText 
            android:id="@+id/ed3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="9"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/bu4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
       >
        <TextView
        android:id="@+id/title4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆环的颜色" 
        />
        <EditText 
            android:id="@+id/ed4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="9"/>
    </LinearLayout>
    <Button 
        android:id="@+id/but5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绘图"/>
    <view.LegePaint
        android:id="@+id/mlegeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值