本篇博客主要介绍android中如何在java代码中绘制矩形框,下面是实现步骤:

一、编写自定义控件DrawImageView:

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.support.annotation.Nullable;import android.util.AttributeSet;public class DrawImageView extends android.support.v7.widget.AppCompatImageView {    public DrawImageView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    Paint paint = new Paint();    {        paint.setAntiAlias(true);//用于防止边缘的锯齿        paint.setColor(Color.BLUE);//设置颜色        paint.setStyle(Paint.Style.STROKE);//设置样式为空心矩形        paint.setStrokeWidth(2.5f);//设置空心矩形边框的宽度        paint.setAlpha(1000);//设置透明度    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawRect(new Rect(100,300,400,600),paint);//绘制矩形,并设置矩形框显示的位置    }}

二、在需要显示矩形框的布局文件中添加自定义的DrawImageView控件:

三、在主布局对应得java文件中实例化自定义的DrawImageView控件:

 div = (DrawImageView) findViewById(R.id.paint_iv); div.draw(new Canvas());

欢迎关注我的公众号:

编程技术与生活(ID:hw_cchang)

android在java代码中绘制矩形框_第1张图片

更多相关文章

  1. Android控件之ImageView,Button, ImageButton
  2. Android Button 控件绑定单击事件
  3. eclipse创建android项目,无法正常预览布局文件
  4. Android 布局中调字体的行距和间距
  5. Android studio相对布局的常用属性

随机推荐

  1. android 常用系统修改和设置
  2. 向SD卡写文件
  3. 2.20 android连接wifi,解决mWifiManager.a
  4. 相机 android java.lang.RuntimeExceptio
  5. appcompat-v7 版本造成的问题No resource
  6. Android 基础布局控件自定义view使用练习
  7. Android中发送短信和彩信
  8. Android开发教程网址
  9. android计算器简单实现代码
  10. Android爬坑-悬浮窗显示位置问题