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應用開發進階Android
- 開發Android系統應用Android
- Android Jetpack - Android TV 應用開發教程AndroidJetpack
- Android 應用程式元件介紹Android元件
- Android 12(S) 圖形顯示系統 - 示例應用(二)Android
- WinRAR初級中級高階等應用
- Harmony 應用開發常用元件介紹元件
- 用於金融科技應用開發的高效能圖表控制元件控制元件
- 快速上手系列--Android應用開發模板Android
- 使用 MPAndroidChat 開發圖表應用Android
- Vue 框架-09-初識元件的應用Vue框架元件
- 金融級應用開發|SOFABoot 框架剖析boot框架
- .NET MAUI 安卓應用開發初體驗UI安卓
- Android開發之跟蹤應用更新大小Android
- Android之五大應用開發框架Android框架
- 運用Kotlin開發Android應用的一些技巧KotlinAndroid
- Jenkins 使用指南 之 初級應用篇Jenkins
- HarmonyOS NEXT應用開發實戰—元件堆疊元件
- C++ Qt開發:SqlTableModel對映元件應用C++QTSQL元件
- 用vscode開發vue應用VSCodeVue
- 一個一線前端攻城獅的快應用開發之路:1、初識快應用前端
- C/C++ Qt TreeWidget 單層樹形元件應用C++QT元件
- PHP開發者超級適用的圖形處理庫PHP
- 鴻蒙 Android iOS 應用開發對比02鴻蒙AndroidiOS
- 開發Web應用Web
- 1.1.4 應用開發
- Android動態更換應用圖示Android
- 手把手docker部署java應用(初級篇)DockerJava
- 【Android開發入門教程】二.Android應用程式結構分析Android
- 大資料技術於應用 視覺化圖表的開發應用大資料視覺化
- Android基本控制元件和Activity的基本應用Android控制元件
- 《反應式應用開發》之“什麼是反應式應用”
- 用VS Code開發Vue應用Vue
- js圖表控制元件:highcharts的應用JS控制元件
- Android 開發者 | 應用相容性注意事項Android
- Android應用開發中如何使用隱藏的APIAndroidAPI
- React元件及應用React元件
- 快應用初體驗
- Cognita: 開源RAG框架助力生產級應用開發框架