国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中自定義View的實現方式總結大全

2019-12-12 02:59:24
字體:
來源:轉載
供稿:網友

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);   } 

總結

以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望奎县| 通州区| 嘉善县| 武宁县| 兴文县| 英德市| 阿拉尔市| 边坝县| 濮阳县| 五河县| 木里| 云浮市| 栾川县| 枣强县| 堆龙德庆县| 博客| 深泽县| 达尔| 临漳县| 新竹县| 安吉县| 彩票| 托克托县| 彰化县| 福建省| 桃园县| 新巴尔虎右旗| 东源县| 阿拉尔市| 桂林市| 仁寿县| 和田市| 玛纳斯县| 乌鲁木齐县| 宁安市| 滦平县| 格尔木市| 临猗县| 穆棱市| 高唐县| 仙游县|