Android繪制機(jī)器人小實(shí)例
本文實(shí)例通過前面學(xué)過的Paint、Canvas等2D繪畫技術(shù)來實(shí)現(xiàn)在手機(jī)屏幕上繪制Android機(jī)器人。
具體代碼實(shí)現(xiàn)和效果:
用來顯示自定義的繪圖類的布局文件
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
打開MainActivity,在文件中創(chuàng)建名為MyView的內(nèi)部類,繼承android.view.View類,并添加構(gòu)造方法和重寫onDraw(Canvas canvas)方法,在里面進(jìn)行作圖:
MainActivity:
package com.example.test;
import android.app.Activity;
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 MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取布局文件中添加的幀布局管理器
FrameLayout ll=(FrameLayout)findViewById(R.id.frameLayout1);
//將自定義的MyView視圖添加到幀布局
ll.addView(new MyView(this));
}
public class MyView extends View{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint();
paint.setAntiAlias(true);//使用抗鋸齒功能
paint.setColor(Color.GREEN);
//繪制機(jī)器人的頭
RectF rectf_head=new RectF(10,10,100,100);
rectf_head.offset(100, 20);//設(shè)置在X軸上偏移100像素,Y軸上偏移20像素
canvas.drawArc(rectf_head, -10, -160, false, paint);//繪制弧
/*.drawArc參數(shù)1:圓的范圍大??;參數(shù)2:起始角度 ;
參數(shù)3:圓心角角度,360為圓,180為半圓;
參數(shù)4:中心 ;參數(shù)5:畫筆Paint;*/
//繪制眼睛
paint.setColor(Color.WHITE);
canvas.drawCircle(135, 53, 4, paint);//繪制圓
canvas.drawCircle(175, 53, 4, paint);//繪制圓
paint.setColor(Color.GREEN);
//繪制天線
paint.setStrokeWidth(2);//設(shè)置觸筆的寬度
canvas.drawLine(120, 15, 135, 35, paint);//繪制線
canvas.drawLine(190, 15, 175, 35, paint);//繪制線
//繪制身體
canvas.drawRect(110,75,200,150, paint);//繪制矩形
RectF rectf_body=new RectF(110,140,200,160);
canvas.drawRoundRect(rectf_body, 10, 10, paint);//繪制圓角矩形
/*參數(shù)說明
rect:RectF對(duì)象。
rx:x方向上的圓角半徑。
ry:y方向上的圓角半徑。
paint:繪制時(shí)所使用的畫筆。*/
//繪制胳膊
RectF rectf_arm=new RectF(85,75,105,140);
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
rectf_arm.offset(120, 0);//設(shè)置在X軸上偏移120像素
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
//繪制腿
RectF rectf_leg=new RectF(125,150,145,200);
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
rectf_leg.offset(40, 0);//設(shè)置在X軸上偏移40像素
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
super.onDraw(canvas);
}
}
}
畫出的效果如圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android用過TextView實(shí)現(xiàn)跑馬燈效果的示例
本篇文章主要介紹了Android用過TextView實(shí)現(xiàn)跑馬燈效果的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Android實(shí)現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法
這篇文章主要介紹了Android實(shí)現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
這篇文章主要為大家詳細(xì)介紹了Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
android設(shè)備不識(shí)別awk命令 缺少busybox怎么辦
這篇文章主要為大家詳細(xì)介紹了android設(shè)備不識(shí)別awk命令,缺少busybox的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
適配AndroidQ拍照和讀取相冊(cè)圖片的實(shí)現(xiàn)方法
這篇文章主要介紹了適配AndroidQ拍照和讀取相冊(cè)圖片的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
這篇文章主要介紹了Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Android Animation之TranslateAnimation(平移動(dòng)畫)
這篇文章主要為大家詳細(xì)介紹了Animation之TranslateAnimation平移動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android實(shí)現(xiàn)自定義View控件的流程詳解
這篇文章主要為大家詳細(xì)介紹了Android中實(shí)現(xiàn)自定義View控件的流程,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06

