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

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程基于自定義view實(shí)現(xiàn)公章效果示例【附源碼下載】

2019-10-22 18:22:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android編程基于自定義view實(shí)現(xiàn)公章效果。分享給大家供大家參考,具體如下:

上次去一個(gè)公司面試,面試官問(wèn)了一個(gè)題,怎么用android的自定義view實(shí)現(xiàn)一個(gè)公章的效果,據(jù)說(shuō)這是華為之前的面試題,我想了下,要是公章的效果,最外層是一個(gè)圓,里面是一個(gè)五角星,但是這文字怎么畫(huà)呢,比較難搞,后來(lái)回來(lái)看了下java的api,發(fā)現(xiàn)人家的Path里面本來(lái)就提供了這么一個(gè)方法:

public void addArc(RectF oval, float startAngle, float sweepAngle) { addArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle);}

然后人家解釋說(shuō)了,根據(jù)狐線的角度生成相應(yīng)的路徑,所以我們就可以給文字設(shè)置一個(gè)相應(yīng)繪制區(qū)域,使其繪制的文字都在這個(gè)區(qū)域內(nèi),

path.addArc(oval,-(firstrad-textPadding*i/2), textPadding);

接下來(lái)我們只需要在這個(gè)區(qū)域內(nèi)把文字繪制上去就行了。

好的,下面是全部代碼:

首先繼承自View,我們?cè)跇?gòu)造里面初始化,同樣為了方便程序的擴(kuò)展性,我們用自定義屬性,

<declare-styleable name="Seal"> <attr name="scale_text_size" format="dimension" /> <attr name="scale_text_color" format="color" /> <attr name="scale_text" format="string" /> <attr name="scale_text_padding" format="float" /> <attr name="circle_stroke_width" format="dimension" /> <attr name="circle_color" format="color" /> <attr name="circle_radius" format="dimension" /></declare-styleable>

然后我們初始化的時(shí)候主要初始化文字,文字大小,文字間距,文字顏色等等,

private void initViews(AttributeSet attrs, int defStyle) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Seal, defStyle, 0); circleText = typedArray.getString(R.styleable.Seal_scale_text); textSize = typedArray.getDimension(R.styleable.Seal_scale_text_size, 20); scaleTextColor = typedArray.getColor(R.styleable.Seal_scale_text_color, getResources().getColor(R.color.c9)); textPadding=typedArray.getFloat(R.styleable.Seal_scale_text_padding,50); circleStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.Seal_circle_stroke_width, 3); circleColor = typedArray.getColor(R.styleable.Seal_circle_color, getResources().getColor(R.color.c9)); circleRadius = typedArray.getDimensionPixelSize(R.styleable.Seal_circle_radius, 7); typedArray.recycle();}

接下來(lái)我們?cè)谥貙?xiě)Ondraww(Canvas canvas)

@Overrideprotected void onDraw(Canvas rootCanvas) { super.onDraw(rootCanvas); Bitmap image = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); Paint paint=new Paint(); drawRing(canvas,paint); drawStar(canvas); drawText(canvas); rootCanvas.drawBitmap(image, 0, 0, null);}

接下來(lái)是對(duì)應(yīng)的三個(gè)方法:畫(huà)圓環(huán)(ring),五角星(star),文字(text)

//圓環(huán)private void drawRing(Canvas canvas, Paint paint) { centre = canvas.getWidth() / 2; // 獲取圓心的x坐標(biāo) radius = (int) (centre - circleStrokeWidth / 2); // 圓環(huán)的半徑 paint.setColor(Color.RED); // 設(shè)置圓環(huán)的顏色 paint.setStyle(Paint.Style.STROKE); // 設(shè)置空心 paint.setStrokeWidth(circleStrokeWidth); // 設(shè)置圓環(huán)的寬度 paint.setAntiAlias(true); // 消除鋸齒 canvas.drawCircle(centre, centre, radius, paint); // 畫(huà)出圓環(huán)}//繪制五角星private void drawStar(Canvas canvas){ float start_radius = (float) ((radius / 2)*1.1); int x = centre, y = centre; float x1,y1,x2,y2,x3,y3,x4,y4,x5,y5; float r72 = (float) Math.toRadians(72); float r36 = (float) Math.toRadians(36); //頂點(diǎn) x1 = x; y1 = y - start_radius; //左1 x2 = (float) (x - start_radius*Math.sin(r72)); y2 = (float) (y - start_radius*Math.cos(r72)); //右1 x3 = (float) (x + start_radius*Math.sin(r72)); y3 = (float) (y - start_radius*Math.cos(r72)); //左2 x4 = (float) (x - start_radius*Math.sin(r36)); y4 = (float) (y + start_radius*Math.cos(r36)); //右2 x5 = (float) (x + start_radius*Math.sin(r36)); y5 = (float) (y + start_radius*Math.cos(r36)); //連接各個(gè)節(jié)點(diǎn),繪制五角星 Path path = new Path(); path.moveTo(x1, y1); path.lineTo(x5, y5); path.lineTo(x2, y2); path.lineTo(x3, y3); path.lineTo(x4, y4); path.close(); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawPath(path, paint);}//文字private void drawText(Canvas canvas){ Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(radius/5+5); //圓弧文字所在矩形范圍 RectF oval=new RectF(0, 0, 2*radius, (float) (2*radius)); //第一個(gè)文字偏移角度,其中padding/2為文字間距 float firstrad = 90 + textPadding * (circleText.length()) / 4 - textPadding/8; for(int i = 0; i < circleText.length(); i++){  Path path = new Path();  //根據(jù)角度生成弧線路徑  path.addArc(oval,-(firstrad-textPadding*i/2), textPadding);  canvas.drawTextOnPath(String.valueOf(circleText.charAt(i)), path, -(float) (radius/3),(float) (radius/3), paint); }}

最后在我們需要的視圖中引用下就好了

<com.xzh.sealmaster.view.SealView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:scale_text_size="16sp" app:scale_text_padding="50" app:scale_text="華為上海有限公司" />

完整實(shí)例代碼點(diǎn)擊此處本站下載

 

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 横山县| 托里县| 曲水县| 峨山| 仪陇县| 菏泽市| 东安县| 宕昌县| 金坛市| 吉安县| 北流市| 宁陕县| 渑池县| 招远市| 石棉县| 婺源县| 温宿县| 英德市| 呼玛县| 宁波市| 石柱| 团风县| 晋宁县| 乐昌市| 乌苏市| 蓝田县| 新河县| 来安县| 襄城县| 太仆寺旗| 郸城县| 湖州市| 正宁县| 萨嘎县| 贺州市| 华阴市| 江孜县| 恭城| 犍为县| 新津县| 江孜县|