Android應用初級開發——Canavas元件圖形應用
首先自定義繼承於View的檢視類MyView,在其中定義Paint(畫筆),重寫onDraw() 方法,在方法中根據任務要求呼叫Canavas的相關方法。
下面介紹一個Demo
需求:
- Function:
- 做一個圓形的紅色按鈕
- 中間有一個白色的數字
- 數字起始為20
- 每點選一次減少1
1、新建一個RedButton.java
package com.geekband.canavasdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
/**
* Function:
* 做一個圓形的紅色按鈕
* 中間有一個白色的數字
* 數字起始為20
* 每點選一次減少1
* Created by Acer on 2016/5/29 0029.
*/
public class RedButton extends View implements View.OnClickListener {
private Paint mPaint;
private Rect mRect;
private int mNumber = 0;
private int mBackgroundColor;
public RedButton(Context context) {
this(context, null);
}
public RedButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RedButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mPaint = new Paint();
mRect = new Rect();
this.setOnClickListener(this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TestRedButton);
mBackgroundColor = typedArray.getColor(R.styleable.TestRedButton_backgroundColor, Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//做一個圓形的紅色按鈕
//設定畫布為紅色
/**
* 畫一個紅色的圓
*/
mPaint.setColor(Color.RED);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint);
//中間有一個白色的數字
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(200);
/**
* mRect是這個數字四周的邊距
*/
String text = String.valueOf(mNumber);
mPaint.getTextBounds(text, 0, text.length(), mRect);
int textWidth = mRect.width();
int textHeight = mRect.height();
canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, mPaint);
}
@Override
public void onClick(View v) {
//每點選一次減少1
if (mNumber > 0) {
mNumber--;
} else {
mNumber = 20;
}
invalidate();
}
}
2、回到activity_main.xml中新增自定義的控制元件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.geekband.canavasdemo.MainActivity">
<com.geekband.canavasdemo.RedButton
android:layout_width="200dp"
android:layout_height="200dp"/>
</RelativeLayout>
3、建立attrs.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestRedButton">
<attr name="backgroundColor" format="color"/>
</declare-styleable>
</resources>
4、MainActivity.java無需做任何改變
5、執行就能看到結果
相關文章
- 混合移動應用開發初級例項
- Android應用開發—Intent元件詳解AndroidIntent元件
- 【HTML5】Android應用開發新路線(用HTML5開發Android應用)HTMLAndroid
- Android應用開發進階Android
- 開發Android系統應用Android
- Web應用的元件化開發(一)Web元件化
- Web應用的元件化開發(二)Web元件化
- WinRAR初級中級高階等應用
- .NET MAUI 安卓應用開發初體驗UI安卓
- Linux下應用程式開發:QT中使用圖形庫(轉)LinuxQT
- Android應用開發-圖片載入庫GlideAndroidIDE
- Android應用開發之(利用好圖片快取)Android快取
- Android應用增量升級Android
- 金融級應用開發|SOFABoot 框架剖析boot框架
- Infinum:開發iOS應用要比開發Android應用節省約30%時間iOSAndroid
- 使用Kotlin開發Android應用KotlinAndroid
- Android應用開發常用知識Android
- Android應用開發筆記(一)Android筆記
- Android應用開發架構概述Android架構
- Android 應用程式元件介紹Android元件
- Android 12(S) 圖形顯示系統 - 示例應用(二)Android
- 用於金融科技應用開發的高效能圖表控制元件控制元件
- Vue 框架-09-初識元件的應用Vue框架元件
- 初識用.NET Remoting來開發分散式應用(轉)REM分散式
- 應用元件元件
- Android Jetpack - Android TV 應用開發教程AndroidJetpack
- 使用 MPAndroidChat 開發圖表應用Android
- 快速上手系列--Android應用開發模板Android
- 如何用PHP開發Android應用程式PHPAndroid
- Android 應用開發推薦書單Android
- 【Android研究院之應用開發】Android
- 書訊:《Android應用開發揭祕》Android
- 在命令列開發 Android 應用程式命令列Android
- Xamarin開發教程如何使用Xamarin開發Android應用Android
- 1.1.4 應用開發
- 開發Web應用Web
- 使用MyEclipse開發Java EE應用:企業級應用程式專案(下)EclipseJava
- 使用MyEclipse開發Java EE應用:企業級應用程式專案(上)EclipseJava