平台
RK3399 + Android 8.1
问题
代码中, 屏幕分辨率为1024x600, 自定义View的尺寸为300x300, 在窗口居中显示. 自定义View绘制的顺序为:
- 绘制白色背景-Canvas.drawColor
- 绘制黑色矩形-Canvas.drawRect
- 绘制红色交叉线, 分别为左上-右下 和 右上-左下 两条
- 绘制四个角圆, 半径为10个像素
效果图: 在代码中,增加了左右拖动旋转控件的功能
上图的虚线, 非代码绘制
在没有旋转控件前, 并未出现图中的虚线, 而且虚线还会根据旋转的角度而产生变化, 虚线的颜色会根据自定义View的背景色发生改变.
源码
canvas_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlRoot"
android:background="#FF000000"/>
CanvasTest.java
package com.android.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.android.test.R;
public class CanvasTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canvas_test);
RelativeLayout rlRoot = (RelativeLayout)findViewById(R.id.rlRoot);
CanvasView cv = new CanvasView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(300, 300);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
rlRoot.addView(cv, lp);
}
class CanvasView extends View {
Paint p_anti = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint p_norm = new Paint();
public CanvasView(Context context) {
super(context);
}
int W, H;
int CX, CY;
RectF viewPort = new RectF();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
W = w;
CX = W/2;
H = h;
CY = H/2;
viewPort.set(0, 0, W, H);
}
float dx, dy, cx, cy;
float rotZ = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
cx = x;
cy = y;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
dx = x;
dy = y;
rotZ = getRotation();
break;
case MotionEvent.ACTION_MOVE:
float rotate = (dx - cx) / W;
setRotation(rotZ + 90f * rotate);
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
Paint p = p_anti;
canvas.drawColor(Color.WHITE);
p.setColor(Color.BLACK);
canvas.drawRect(viewPort, p);
p.setColor(Color.RED);
float[] lines = {viewPort.left, viewPort.top, viewPort.right, viewPort.bottom,
viewPort.right, viewPort.top, viewPort.left, viewPort.bottom};
canvas.drawLines(lines, p);
for(int i = 0; i < lines.length / 2; i ++){
canvas.drawCircle(lines[i * 2], lines[i * 2 + 1], 10, p);
}
}
}
}
最终发现, 这是由Paint引起的. 在代码中定义了两个Paint
Paint p_anti = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint p_norm = new Paint();
在onDraw函数中, 使用p_norm并不会出现虚线的问题, 而使用p_anti才会出现. 放大图像:
|