Android自定义时间轴的实现过程

 更新时间:2017年01月25日 10:33:19   作者:zhiyuan0932  
这篇文章主要介绍了Android自定义时间轴的实现过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文讲述Android自定义时间轴的实现过程,供大家参考,具体内容如下

相关视频链接:
Android自定义控件系列
http://edu.csdn.net/course/detail/3719/65396
Android视频全系列
http://edu.csdn.net/course/detail/2741/43163

时间轴效果,实际上非常简单,就是listView中一个又一个的条目而已….大家可以只关注一个条目。
首先展示一个条目的布局效果

<?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中的代码–就是一个ListView的效果展示

public class MainActivity extends Activity {

 private ListView listView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 listView = (ListView) findViewById(R.id.listView);
 initData();
 listView.setAdapter(new MyBaseAdapter());
 }

 class MyBaseAdapter extends BaseAdapter {

 @Override
 public int getCount() {
 return dataList.size();
 }

 @Override
 public Object getItem(int arg0) {
 return dataList.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
 return arg0;
 }

 @Override
 public View getView(int arg0, View arg1, ViewGroup arg2) {

 View view = View.inflate(MainActivity.this, R.layout.item, null);
 TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
 TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
 TimeView timeView = (TimeView) view.findViewById(R.id.timeView);
 timeView.setTime(dataList.get(arg0).getTime());
 tv_content.setText(dataList.get(arg0).getContent());
 tv_time.setText(dataList.get(arg0).getTime());

 return view;
 }

 }

 ArrayList<DataBean> dataList = new ArrayList<DataBean>();

 private void initData() {
 for (int i = 0; i < 20; i++) {
 dataList.add(new DataBean("哈哈哈哈" + i, "25/10"));
 }

 }

}

好了,这样的一个自定义时间轴效果就搞定了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android创建简单发送和接收短信应用

    Android创建简单发送和接收短信应用

    收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持。今天我们开始自己创建一个简单的发送和接收短信的应用,需要的朋友可以参考下
    2016-04-04
  • Android应用获取设备序列号的方法

    Android应用获取设备序列号的方法

    本篇文章主要介绍了Android应用获取设备序列号的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Android Notes思码逸问题处理记录

    Android Notes思码逸问题处理记录

    这篇文章主要介绍了Android Notes思码逸问题处理记录详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android入门计算器编写代码

    Android入门计算器编写代码

    这篇文章主要为大家详细介绍了Android入门计算器编写代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 在Android线程池里运行代码任务实例

    在Android线程池里运行代码任务实例

    这篇文章主要介绍了在Android线程池里运行代码任务实例,同时介绍了线程池中停止任务的方法,需要的朋友可以参考下
    2014-06-06
  • 关于Kotlin的自动类型转换详解

    关于Kotlin的自动类型转换详解

    这篇文章主要给大家介绍了关于Kotlin的自动类型转换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Android以对话框形式制作数字软键盘示例

    Android以对话框形式制作数字软键盘示例

    大家好,本篇文章主要讲的是Android以对话框形式制作数字软键盘示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • 详解dex优化对Arouter查找路径的影响

    详解dex优化对Arouter查找路径的影响

    dex简单说就是优化后的android版.exe。每个apk安装包里都有。相对于PC上的java虚拟机能运行.class,android上的Davlik虚拟机能运行.dex。本文将着重介绍dex优化对Arouter查找路径的影响
    2021-06-06
  • android动态加载布局文件示例

    android动态加载布局文件示例

    这篇文章主要介绍了android动态加载布局文件示例,需要的朋友可以参考下
    2014-03-03
  • Android 获取服务器与客户端时差的实例代码

    Android 获取服务器与客户端时差的实例代码

    下面小编就为大家分享一篇Android 获取服务器与客户端时差的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01

最新评论