自定义控件(8)---PathEffect平滑曲线--点、线、矩形、圆

自定义控件(8)---PathEffect平滑曲线--点、线、矩形、圆_第1张图片

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <com.aigestudio.customviewdemo.views.PathEffectView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MeasureUtil

package com.aigestudio.customviewdemo.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;

public final class MeasureUtil {
	public static int[] getScreenSize(Activity activity) {
		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return new int[] { metrics.widthPixels, metrics.heightPixels };
	}

}

MainActivity

package com.aigestudio.customviewdemo.activities;

import android.app.Activity;
import android.os.Bundle;

import com.aigestudio.customviewdemo.R;

public class MainActivity extends Activity {

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

PathEffectView

package com.aigestudio.customviewdemo.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

/**
 * PathEffect
 */
@SuppressLint({ "NewApi", "DrawAllocation" })
public class PathEffectView extends View {
	private Paint mPaint;// 画笔对象
	private Path mPath;// 路径对象
	private PathEffect mEffects;// 路径效果数组

	public PathEffectView(Context context, AttributeSet attrs) {
		super(context, attrs);

		/*
		 * 实例化画笔并设置属性
		 * 描边、宽度、颜色
		 */
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(5);
		mPaint.setColor(Color.DKGRAY);

		// 实例化路径
		mPath = new Path();

		// 定义路径的起点
		mPath.moveTo(0, 420);

		// 定义路径的各个点
		for (int i = 0; i <= 30; i++) {
			mPath.lineTo(i * 35, (float) (Math.random() * 300));
		}

		// 创建路径效果数组
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		/*
		 * 实例化各类特效
		 */
		mEffects = new CornerPathEffect(50);

		/*
		 * 绘制路径
		 */
		mPaint.setPathEffect(mEffects);
		canvas.drawPath(mPath, mPaint);

		// 刷新偏移值并重绘视图实现动画效果
		invalidate();
	}
}


========================================

自定义控件(8)---PathEffect平滑曲线--点、线、矩形、圆_第2张图片

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.try_paint_blog.MainActivity" >


</FrameLayout>

MainActivity

package com.example.try_paint_blog;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      setContentView(R.layout.activity_main);
      
      FrameLayout root=(FrameLayout)findViewById(R.id.root);
      root.addView(new MyView(MainActivity.this));
    }

}

MyView

package com.example.try_paint_blog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

public class MyView extends View {

	Context m_context;

	public MyView(Context context) {
		super(context);

		m_context = context;
	}

	// 重写OnDraw()函数,在每次重绘时自主实现绘图
	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Paint paint = new Paint();
		paint.setColor(Color.RED); // 设置画笔颜色
		paint.setStyle(Style.FILL);// 设置填充样式
		paint.setStrokeWidth(5);// 设置画笔宽度
		// 画直线
		canvas.drawLine(0, 10, 100, 10, paint);

		// 两两一直线
		float[] pts = { 0, 50, 20, 50, 40, 50, 60, 50 };
		canvas.drawLines(pts, paint);
		// 画点
		canvas.drawPoint(100, 100, paint);

		// 多个点float drawPoints里路过前两个数值,即第一个点横纵坐标,画出后面四个数值代表的点,即第二,第三个点
		float[] ptss = { 200, 100, 300, 100, 400, 100, 500, 100 };
		canvas.drawPoints(ptss, 2, 4, paint);

		// 矩形
		canvas.drawRect(0, 200, 100, 300, paint);// 直接构造

		RectF rect = new RectF(150, 200, 250, 300);
		canvas.drawRect(rect, paint);// 使用RectF构造

		Rect rect2 = new Rect(300, 200, 400, 300);
		canvas.drawRect(rect2, paint);// 使用Rect构造
		// 圆角
		RectF rect3 = new RectF(0, 350, 200, 450);
		canvas.drawRoundRect(rect3, 20, 20, paint);
		// 圆
		canvas.drawCircle(400, 400, 50, paint);

		// 三角形
		paint.setStyle(Style.STROKE);// 填充样式改为描边
		Path path = new Path();
		path.moveTo(10, 500); // 设定起始点
		path.lineTo(10, 600);// 第一条直线的终点,也是第二条直线的起点
		path.lineTo(50, 600);// 画第二条直线
		path.close();// 闭环

		canvas.drawPath(path, paint);
	}

}



你可能感兴趣的:(自定义控件(8)---PathEffect平滑曲线--点、线、矩形、圆)