1. 程式人生 > >Android 自定義view 八卦圖

Android 自定義view 八卦圖

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import 
android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; /** * Created by 知足 on 2017/11/30. */ public class TaiView extends View implements View.OnClickListener { private RotateAnimation rotateAnimation; private int width = 360; private int height = 360; private int
padding = 5; private Paint mPaint; private RectF mRectf; private RectF blackHalfRect; private RectF whiteHalfRect; public TaiView(Context context) { this(context,null); } public TaiView(Context context, AttributeSet attrs) { this(context, attrs,0); } public TaiView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); initAnim(); setOnClickListener(this); } /** * 初始化畫筆 */ private void initPaint() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(5); } private void initAnim() { //以view的中心點為旋轉參考點 rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setRepeatCount(-1); rotateAnimation.setFillAfter(false); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(width,height); mRectf = new RectF(0,0,width,width); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawCirCle(canvas); drawHalfCirCle(canvas); drawSmallCircle(canvas); } /** * 繪製二個小圓點 * @param canvas */ private void drawSmallCircle(Canvas canvas) { mPaint.setColor(Color.WHITE); mPaint.setStyle(Paint.Style.FILL); canvas.drawCircle(width/2,width/4,20,mPaint); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.FILL); canvas.drawCircle(width/2,width/4*3,20,mPaint); } /** * 繪製二個半圓 一個黑色 一個白色 * @param canvas */ private void drawHalfCirCle(Canvas canvas) { //畫上面黑色半圓 mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.BLACK); blackHalfRect = new RectF(width/4,0,width/2+width/4,width/2); canvas.drawArc(blackHalfRect,270,180,true,mPaint); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.WHITE); whiteHalfRect = new RectF(width/4,width/2,width/2+width/4,width); canvas.drawArc(whiteHalfRect,270,-180,true,mPaint); } /** * 畫一個簡單的圓 * @param canvas */ private void drawCirCle(Canvas canvas) { mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.WHITE); canvas.drawArc(mRectf,270,180,true,mPaint); mPaint.setColor(Color.BLACK); canvas.drawArc(mRectf,270,-180,true,mPaint); } @Override public void onClick(View view) { rotateAnimation.setDuration(1000); rotateAnimation.setInterpolator(new LinearInterpolator());//不停頓 startAnimation(rotateAnimation); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.taijibaguatu.MainActivity">


    <com.example.taijibaguatu.TaiView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</LinearLayout>

相關推薦

Android 定義view 八卦

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity {

Android定義View實現類似車來了軌跡

總體分析下:水平方向recyclewview,item包含定位點,站臺位置和站臺名稱。 下面看實現: 1.繼承framelayout,實現構造方法: public class BusStopPlateView extends FrameLayout { ... public

Android定義View——弧線展示

前面我也寫了有幾個自定義進度的控制元件,那麼,今天,我再加一個控制元件,原理跟前面講的差不多,先看看效果: 這個是一個以弧線為依託的進度控制元件,主要包括了兩個圓弧、一個圓、一個文字。 當我們點選開始按鈕的時候,會出現一個動畫,逐漸的出現進度,好了,下面

android:定義view--區線

老規矩,先上圖,整個自定義view分為標題模組,xy軸標註資料模組和資料模組,其實資料模組是有一個淺灰色的背景,但是動態圖裡面看不出來,真機上可以看出;和前面幾個圖表相比增加了貝塞爾區線,虛線,手勢點選顯示標註線和當前日資料;後面還有左右按鈕切換上週下週資料,這個不是主要功能

Android 定義View實現圓形切的效果

使用自定義View實現圓形ImageView的效果 目前圓形邊框還需要調整,這裡有點問題 實現思路 使用一個Paint,將得到的Bitmap設定成paint的Shader,設定完成後,使用Matrix調整圖片至居中,使用RectF約束邊框,最後完成

Android定義View實現簡單的折線、柱狀

首先說第一個柱狀圖,實現很簡單。一個自定義View,重現裡面的OnDraw方法。然後利用paint,canvas繪製帶填充的長方形即可。每個長方形的X軸平方View的x軸即可,長方形的高度通過簡單的計算即可得到。下面上柱狀圖程式碼 package com.hrules.

Android 定義View練習:雷達(比重)繪製

code: package com.louisgeek.louiscustomviewstudy; import android.content.Context; import android.content.res.Resources; import

Android 定義View,實現折線

最近要完成一個折線圖控制元件,用來顯示一系列的狀態,並可以進行滑動。雖然現在有很多大牛寫好的控制元件可以直接使用,但我感覺那些控制元件是給高手的使用的,對於我這樣的菜鳥,還是腳踏實地,自己慢慢碼程式碼,才可以提高。下面就是結果圖(每種狀態用一個表情圖片表示): 1 主頁

Android定義View——彩色圓環統計

效果展示實現步驟1、初始化變數 //-------------必須給的資料相關------------- private String[] str = new String[]{"一年級", "二年級", "三年級", "四年級", "五年級", "六年級"};

android定義View,實現折線(二)

效果圖: LineChartView類: public class LineChartView extends View { private int width; private int height; private float maxVal

android 定義view 之 動態音訊 (二)

還是和上一篇一樣,先上效果圖 這次要比上次難了點, 首先我們把自定義檔案貼上 /** * 作者:chengxiangtong on 2016/11/10 14:44 * 郵箱:[email protected] * 仿音訊條 */ public cl

android:定義view--橫向柱形

此demo也是從我們真實專案中copy出來的;大致步驟和製作MyTabView差不多,只不過稍微繁瑣一些; /** * Created by zheng on 2017/12/4. * 橫向的柱形圖 * * 1:繪製XY軸 * 2:繪製XY軸刻度和旁邊的文字

Android定義View:帶背景顏色的TextView和條形--(1)

初始: 最近在看《Android群英傳》一書,程式碼自己敲了一遍,想想之前敲了又忘記的慘痛經歷,決定在部落格上記錄自己敲的程式碼,有幾個寫幾篇,放在一個系列裡邊,就這樣,以後看就能一下子找到了。 自定義View 自定義View我們大致可以從是三個方面著手:

Android定義View,畫一個好看帶延長線的餅狀

開發十年,就只剩下這套架構體系了! >>>   

Android 定義View

wid declare created odi lex getwidth 實現 tdi led   最近在看鴻洋大神的博客,在看到自定義部分View部分時,想到之前案子中經常會要用到"圖片 + 文字"這類控件,如下圖所示: 之前的做法是在布局文件中,將一個Imag

Android定義view詳解

this boolean mar 處理 都是 並且 jdk text 命名 從繼承開始 懂點面向對象語言知識的都知道:封裝,繼承和多態,這是面向對象的三個基本特征,所以在自定義View的時候,最簡單的方法就是繼承現有的View 通過上面這段代碼,我定義了一個Ske

Android -- 定義view實現keep歡迎頁倒計時效果

super onfinish -m use new getc awt ttr alt 1,最近打開keep的app的時候,發現它的歡迎頁面的倒計時效果還不錯,所以打算自己來寫寫,然後就有了這篇文章。 2,還是老規矩,先看一下我們今天實現的效果   相較於我們常見的倒計時

Android定義View效果目錄

class 重寫 自定義 textview 居中 url 冒泡 and 雷達圖 1、絢麗的loading動效的實現 2、Android自定義View:進度條+冒泡文本 3、Android雷達圖(蜘蛛網圖) 4、Android文本閃爍 5、Android繪制圓形進度條 6、重

Android定義View——實現水波紋效果類似剩余流量球

string 三個點 pre ber block span 初始化 move 理解 最近突然手癢就想搞個貝塞爾曲線做個水波紋效果玩玩,終於功夫不負有心人最後實現了想要的效果,一起來看下吧: 效果圖鎮樓 一:先一步一步來分解一下實現的過程 需要繪制一個正弦曲線(sin

Android 定義 View 知識點

移動 encode swe em1 red 鋸齒 枚舉類 map() tex 根據 Hencoder 提供的知識點,進行學習和總結。 三個要點: 布局 繪制 觸摸反饋 繪制 自定義繪制:由自己實現繪制過程 常用繪制方法 onDraw(Canvas canvas) 繪制