本篇文章為大家展示了如何在Android應(yīng)用中實(shí)現(xiàn)自定義View,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供合水企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、HTML5、小程序制作等業(yè)務(wù)。10年已為合水眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
Android自定義view的種類
自定義view大概可以分為四個(gè)大類,主要是通過(guò)實(shí)現(xiàn)方式來(lái)區(qū)分
1.自繪控件,繼承view,重寫onDraw方法,在其中進(jìn)行繪制,需要自己適配邊距等等
2.繼承ViewGroup派生的特殊Layout,主要用于實(shí)現(xiàn)自定義布局,也需要自己適配邊距等
3.繼承特定的View(如TextView等),不用自己適配支持wrap_conten,match_parent,可以給其加入新的功能
4.繼承特定的ViewGroup,例如linearlayout,多用于多個(gè)控件的組合view,也不用自己去做適配
自繪控件
這種自定義view是最復(fù)雜的一種,因?yàn)榧纫m配wrap_conten,match_parent又要通過(guò)條件判斷來(lái)在屏幕上繪制不同的內(nèi)容,主要就是重寫onDraw方法
以下是一個(gè)簡(jiǎn)單的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); }
關(guān)于這個(gè)例子的完整版本,請(qǐng)查看另外一篇文章點(diǎn)擊這里
繼承ViewGroup派生的特殊Layout
主要是通過(guò)在方法中加載特定的布局,在對(duì)其內(nèi)部的各個(gè)view的行為進(jìn)行指定來(lái)實(shí)現(xiàn)。
繼承特定的View(如TextView等)
可以增加特定view對(duì)特定事件的響應(yīng)
繼承指定ViewGroup的view
也是通過(guò)加載特定布局,再在其中處理view的行為來(lái)實(shí)現(xiàn),大部分繼承ViewGroup的自定義view都可以用此方法實(shí)現(xiàn),不過(guò)viewgroup的方式更接近底層。
一個(gè)簡(jiǎn)單的例子
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); }
上述內(nèi)容就是如何在Android應(yīng)用中實(shí)現(xiàn)自定義View,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:如何在Android應(yīng)用中實(shí)現(xiàn)自定義View
文章來(lái)源:http://www.muchs.cn/article14/jpjide.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航、云服務(wù)器、做網(wǎng)站、微信公眾號(hào)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)