国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義時間軸的實現(xiàn)過程

2019-12-12 03:52:03
字體:
供稿:網(wǎng)友

本文講述Android自定義時間軸的實現(xiàn)過程,供大家參考,具體內(nèi)容如下

相關(guān)視頻鏈接:
Android自定義控件系列
http://edu.csdn.net/course/detail/3719/65396
Android視頻全系列
http://edu.csdn.net/course/detail/2741/43163

時間軸效果,實際上非常簡單,就是listView中一個又一個的條目而已….大家可以只關(guān)注一個條目。
首先展示一個條目的布局效果

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" > <!-- 線條部分 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingLeft="30dp" > <View android:layout_width="3dp" android:layout_height="20dp" android:background="#88000000" /> <com.example.time.TimeView android:src="@drawable/ic_launcher" android:id="@+id/timeView" android:layout_width="40dp" android:layout_height="40dp" /> <View android:layout_width="3dp" android:layout_height="40dp" android:background="#88000000" /> </LinearLayout> <!-- 文字部分 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp" > <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈哈哈" android:textColor="#ABABAB" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_content" android:text="時間" android:textColor="#ABABAB" /> </LinearLayout></LinearLayout>

接下來看一下自定義的TimeView如何書寫

package com.example.time;import java.util.Random;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.graphics.Rect;import android.util.AttributeSet;import android.view.View;public class TimeView extends View { private Random random; private String time; private Rect mBounds = new Rect(); private int rgb; public TimeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public TimeView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public TimeView(Context context) { super(context); initView(); } private void initView() { random = new Random(); //定義顏色---這里純粹為了好玩--大家定義的時候可以在自定義控件外邊定義,將顏色傳遞進來 rgb = Color.rgb(100+random.nextInt(155), 100+random.nextInt(155), random.nextInt(100+155)); } public void setTime(String time) { this.time = time; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(rgb); paint.setAntiAlias(true); paint.setStyle(Style.FILL_AND_STROKE); //先繪制圓 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint); paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(30); paint.getTextBounds(time, 0, time.length(), mBounds); float textWidth = mBounds.width(); float textHeight = mBounds.height(); //再繪制文字 canvas.drawText(time, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, paint); }}

看一下Activity中的代碼

主站蜘蛛池模板: 宁武县| 吉安县| 江陵县| 色达县| 若尔盖县| 保靖县| 垣曲县| 黄石市| 新竹县| 丽水市| 乌什县| 勃利县| 新干县| 闽清县| 和龙市| 林州市| 枣庄市| 德州市| 迁安市| 卢湾区| 宣威市| 诸城市| 都江堰市| 浏阳市| 明星| 宁海县| 资阳市| 石台县| 贺州市| 渭源县| 崇礼县| 同江市| 康乐县| 贺州市| 夏津县| 阳城县| 水城县| 定襄县| 郓城县| 南投市| 文安县|