Android涓娇鐢–anvas鍜孭aint缁樺埗涓€涓畨鍗撴満鍣ㄤ汉

古人已用三冬足,年少今开万卷余。这篇文章主要讲述Android涓娇鐢–anvas鍜孭aint缁樺埗涓€涓畨鍗撴満鍣ㄤ汉相关的知识,希望能为你提供帮助。
鏍囩锛?a href='http://www.mamicode.com/so/1/xtend' title='xtend'>xtend      鐢靛瓙      image      娣诲姞      ref      state      contex      idg      addview     
鍦烘櫙鍦ˋndroid涓敾绗斾娇鐢≒aint绫伙紝鐢诲竷浣跨敤Canvas绫绘潵琛ㄧず銆?/p>缁樺浘鐨勫熀鏈楠?/p>棣栧厛缂栧啓涓€涓户鎵胯嚜View鐨勮嚜瀹氫箟View绫伙紝鐒跺悗閲嶅啓鍏秓nDraw鏂规硶锛屾渶鍚庢妸鑷畾涔夌殑view娣诲姞鍒癮ctvity涓€?/p> 鏁堟灉

Android涓娇鐢–anvas鍜孭aint缁樺埗涓€涓畨鍗撴満鍣ㄤ汉

文章图片

 
 
Android涓娇鐢–anvas鍜孭aint缁樺埗涓€涓畨鍗撴満鍣ㄤ汉
鐒跺悗鍦ˋCtivity涓柊寤鸿嚜瀹氫箟View绫伙紝杩欓噷鏄疢yView浣垮叾缁ф壙View绫诲苟缂栧啓涓€涓甫涓€涓弬鏁扮殑鏋勯€犳柟娉曞拰閲嶅啓onDraw鏂规硶銆?/p>【Android涓娇鐢–anvas鍜孭aint缁樺埗涓€涓畨鍗撴満鍣ㄤ汉】鍦ㄩ噸鍐欑殑onDraw鏂规硶涓柊寤虹敾绗斿苟璁剧疆涓€浜涘睘鎬э紝鐒跺悗浣跨敤鐢荤瑪鍦ㄧ敾甯冧笂鎸囧畾鐨勪綅缃粯鍒跺浘褰€?/p>
package com.badao.alarmmanager; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class RobitActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_robit); FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout); //鑾峰彇甯у竷灞€绠$悊鍣?/span> frameLayout.addView(new MyView(this)); //灏嗚嚜瀹氫箟瑙嗗浘鐨勫唴閮ㄧ被娣诲姞鍒板竷灞€绠$悊鍣ㄤ腑 }private class MyView extends View { public MyView(Context context) { super(context); }@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(); //榛樿璁剧疆鍒涘缓涓€涓敾绗?/span> paint.setAntiAlias(true); //浣跨敤鎶楅敮榻垮姛鑳?/span> paint.setColor(0xFFA4C739); //璁剧疆鐢荤瑪鐨勯鑹蹭负缁胯壊 //缁樺埗鏈哄櫒浜虹殑澶?/span> RectF rectf_head=new RectF(10, 10, 100, 100); rectf_head.offset(90, 20); canvas.drawArc(rectf_head, -10, -160, false, paint); //缁樺埗寮?//缁樺埗鐪肩潧 paint.setColor(Color.WHITE); //璁剧疆鐢荤瑪鐨勯鑹蹭负鐧借壊 canvas.drawCircle(125, 53, 4, paint); //缁樺埗鍦?/span> canvas.drawCircle(165, 53, 4, paint); //缁樺埗鍦?/span> paint.setColor(0xFFA4C739); //璁剧疆鐢荤瑪鐨勯鑹蹭负缁胯壊 //缁樺埗澶╃嚎 paint.setStrokeWidth(2); //璁剧疆绗旇Е鐨勫搴?/span> canvas.drawLine(110, 15, 125, 35, paint); //缁樺埗绾?/span> canvas.drawLine(180, 15, 165, 35, paint); //缁樺埗绾?//缁樺埗韬綋 canvas.drawRect(100, 75, 190, 150, paint); //缁樺埗鐭╁舰 RectF rectf_body=new RectF(100,140,190,160); canvas.drawRoundRect(rectf_body, 10, 10, paint); //缁樺埗鍦嗚鐭╁舰 //缁樺埗鑳宠唺 RectF rectf_arm=new RectF(75,75,95,140); canvas.drawRoundRect(rectf_arm, 10, 10, paint); //缁樺埗宸︿晶鐨勮兂鑶?/span> rectf_arm.offset(120, 0); //璁剧疆鍦╔杞翠笂鍋忕Щ120鍍忕礌 canvas.drawRoundRect(rectf_arm, 10, 10, paint); //缁樺埗鍙充晶鐨勮兂鑶?//缁樺埗鑵?/span> RectF rectf_leg=new RectF(115,150,135,200); canvas.drawRoundRect(rectf_leg, 10, 10, paint); //缁樺埗宸︿晶鐨勮吙 rectf_leg.offset(40, 0); //璁剧疆鍦╔杞翠笂鍋忕Щ40鍍忕礌 canvas.drawRoundRect(rectf_leg, 10, 10, paint); //缁樺埗鍙充晶鐨勮吙 } } }


    推荐阅读