在android设备中查看检测屏幕是否触摸正常时,通常会采用在页面使用手指触摸,那我们就来完成一下这个效果。
如图所示:
首先页面的布局很简单只需要一个全屏的布局即可,这个我们还增加了一个title,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dd">
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#0099ff"
android:text="划线测试 手指抬起不清除"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
接下来就是对应的activity代码,实现了一个自定义的view,使用到了手指的触摸,然后实现canvas绘图效果,在处理一下当触摸按下时的动作逻辑,让标题VISIBLE或者GONE即可全屏显示,又能显示标题。
public class LineActivity2 extends AppCompatActivity {
private int width;
private int height;
private LinearLayout llbg;
private LinearLayout llTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager windowManager = getWindowManager();
width = windowManager.getDefaultDisplay().getWidth();
height = windowManager.getDefaultDisplay().getHeight();
setContentView(R.layout.activity_line2);
llbg=findViewById(R.id.ll_bg);
llTitle=findViewById(R.id.ll_title);
llbg.addView(new Drawl(this));
}
class Drawl extends View {
private int mov_x;
private int mov_y;
private Paint paint;
private Canvas canvas;
private Bitmap bitmap;
private int blcolor;
public Drawl(Context context) {
super(context);
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas=new Canvas();
canvas.setBitmap(bitmap);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(30);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap,0,0,null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_MOVE) {
canvas.drawLine(mov_x, mov_y, event.getX(), event.getY(), paint);
invalidate();
llTitle.setVisibility(GONE);
}
if (event.getAction()==MotionEvent.ACTION_DOWN) {
mov_x=(int) event.getX();
mov_y=(int) event.getY();
invalidate();
llTitle.setVisibility(GONE);
}
if(event.getAction()==MotionEvent.ACTION_UP){
llTitle.setVisibility(VISIBLE);
}
mov_x=(int) event.getX();
mov_y=(int) event.getY();
return true;
}
}
}
|