Android中自定义View的实现方式总结大全

作者:u3coding 时间:2023-02-03 08:55:55 

Android自定义view是什么

在我们的日常开发中,很多时候系统提供的view是无法满足我们的需求的,例如,我们想给一个edittext加上清除按钮,等等。
这时候我们就需要对系统的view进行扩展或者组合,这就是所谓的自定义view。

Android自定义view的种类

自定义view大概可以分为四个大类,主要是通过实现方式来区分

      1.自绘控件,继承view,重写onDraw方法,在其中进行绘制,需要自己适配边距等等

      2.继承ViewGroup派生的特殊Layout,主要用于实现自定义布局,也需要自己适配边距等

      3.继承特定的View(如TextView等),不用自己适配支持wrap_conten,match_parent,可以给其加入新的功能

      4.继承特定的ViewGroup,例如linearlayout,多用于多个控件的组合view,也不用自己去做适配

自绘控件

这种自定义view是最复杂的一种,因为既要适配wrap_conten,match_parent又要通过条件判断来在屏幕上绘制不同的内容,主要就是重写onDraw方法

以下是一个简单的onDraw重写代码


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

final int paddingLeft = getPaddingLeft();
 final int paddingRight = getPaddingRight();
 final int paddingTop = getPaddingTop();
 final int paddingBottom = getPaddingBottom();

//get the view's width and height and decide the radiu
 int width = getWidth() - paddingLeft - paddingRight;
 int height = getHeight() - paddingTop - paddingBottom;
 radiu = Math.min(width , height) / 2 - boundWidth - progressWidth;

//setup the paint
 paint.setStyle(Paint.Style.STROKE);
 paint.setStrokeWidth(boundWidth);
 paint.setColor(Color.BLACK);

//draw the inner circle
 int centerX = paddingLeft + getWidth()/2;
 int centerY = paddingTop + getHeight() / 2;
 canvas.drawCircle(centerX,centerY, radiu, paint);

float totalRadiu = radiu +boundWidth +progressWidth/2;

//draw the circlr pic
 if (drawable != null&&bitmap == null) {
  image = ((BitmapDrawable) drawable).getBitmap();

bitmap = Bitmap.createBitmap((int)(2*totalRadiu),(int)(2*totalRadiu), Bitmap.Config.ARGB_8888);
  Canvas bitmapCanvas = new Canvas(bitmap);

Paint bitmapPaint = new Paint();
  bitmapPaint.setAntiAlias(true);

bitmapCanvas.drawCircle(totalRadiu, totalRadiu, radiu, bitmapPaint);

bitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  bitmapCanvas.drawBitmap(image,null,new RectF(0,0,2*totalRadiu,2*totalRadiu) , bitmapPaint);

}
 Rect rect = new Rect((int)(centerX -totalRadiu),(int)(centerY-totalRadiu),(int)(centerX+totalRadiu),(int)(centerY+ totalRadiu));
 canvas.save();
 if(isRotate)
 canvas.rotate(rotateDegree,centerX,centerY);
 canvas.drawBitmap(bitmap,null ,rect, paint);

canvas.restore();
 //set paint for arc
 paint.setStrokeWidth(progressWidth);
 paint.setStrokeCap(Paint.Cap.ROUND);

//prepare for draw arc
 RectF oval = new RectF();
 oval.left = centerX -totalRadiu ;
 oval.top =centerY- totalRadiu ;
 oval.right = centerX + totalRadiu;
 oval.bottom = centerY+ totalRadiu;
 paint.setColor(progressBackColor);

//draw background arc
 canvas.drawArc(oval, arcStar, arcEnd, false, paint);

//draw progress arc
 paint.setColor(progressColor);
 canvas.drawArc(oval, arcStar, progress, false, paint);
}

关于这个例子的完整版本,请查看另外一篇文章点击这里

继承ViewGroup派生的特殊Layout

主要是通过在方法中加载特定的布局,在对其内部的各个view的行为进行指定来实现。

继承特定的View(如TextView等)

可以增加特定view对特定事件的响应

继承指定ViewGroup的view

也是通过加载特定布局,再在其中处理view的行为来实现,大部分继承ViewGroup的自定义view都可以用此方法实现,不过viewgroup的方式更接近底层。

一个简单的例子


public MyView(Context context, AttributeSet attrs) {
 super(context, attrs);
 LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 inflater.inflate(R.layout.imagebtn, this);
 imageView=(ImageView) findViewById(R.id.imageView1);
 textView=(TextView)findViewById(R.id.textView1);  
}

来源:http://www.u3coding.com/2017/04/25/android自定义view的实现方式总结/

标签:android,自定义view
0
投稿

猜你喜欢

  • Spring MVC项目中log4J和AOP使用详解

    2022-11-16 08:36:29
  • Android打开GPS导航并获取位置信息返回null解决方案

    2021-08-31 09:21:19
  • Mybatis-Plus环境配置与入门案例分析

    2022-02-05 01:15:01
  • unity使用socket编程实现聊天室功能

    2023-10-18 05:26:04
  • 详解使用Spring Security OAuth 实现OAuth 2.0 授权

    2023-10-01 23:05:10
  • Spring@Value使用获取配置信息为null的操作

    2021-08-01 23:46:27
  • Android编程之控件ListView使用方法

    2022-08-12 08:50:03
  • Java模拟死锁发生之演绎哲学家进餐问题案例详解

    2022-10-09 22:52:44
  • 如何用C#实现SAGA分布式事务

    2022-11-29 20:34:49
  • Java的Struts框架中<results>标签的使用方法

    2022-04-16 16:08:22
  • java基于swing实现的五子棋游戏代码

    2023-09-24 17:31:17
  • Java实现简单通讯录管理系统

    2022-06-28 01:15:54
  • Spring boot事件监听实现过程解析

    2022-08-29 13:46:02
  • Java对象转json JsonFormat注解

    2022-08-27 00:44:09
  • 详解Mybatis框架SQL防注入指南

    2023-09-16 02:49:02
  • java ArrayList集合中的某个对象属性进行排序的实现代码

    2022-05-27 19:54:24
  • 解决C# winForm自定义鼠标样式的两种实现方法详解

    2021-07-15 17:10:46
  • C# 表达式目录树Expression的实现

    2023-04-03 22:57:32
  • java中删除 数组中的指定元素方法

    2023-02-02 12:45:59
  • android自定义滚动上下回弹scollView

    2023-05-19 13:10:40
  • asp之家 软件编程 m.aspxhome.com