全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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使用自定义View实现横行时间轴效果  # Android自定义view仿淘宝快递物流信息时间轴  # Android实现快递物流时间轴效果  # Android实现列表时间轴  # Android自定义指示器时间轴效果实例代码详解  # 教你3分钟了解Android 简易时间轴的实现方法  # Android自定义View实现垂直时间轴布局  # Android控件之使用ListView实现时间轴效果  # android自定义控件实现简易时间轴(2)  # 自定义  # 看一下  # 好了  # 哈哈哈哈  # 全系列  # 具体内容  # 大家多多  # 搞定了  # src  # drawable  # TimeView  # ic_launcher  # paddingRight  # paddingTop  # id 


相关文章: 定制建站策划方案_专业建站与网站建设方案一站式指南  如何使用Golang安装API文档生成工具_快速生成接口文档  c# 服务器GC和工作站GC的区别和设置  湖北网站制作公司有哪些,湖北清能集团官网?  Swift中swift中的switch 语句  如何制作一个表白网站视频,关于勇敢表白的小标题?  定制建站如何定义?其核心优势是什么?  ,柠檬视频怎样兑换vip?  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  h5在线制作网站电脑版下载,h5网页制作软件?  如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法  建站之星安装后如何自定义网站颜色与字体?  个人摄影网站制作流程,摄影爱好者都去什么网站?  Python文件管理规范_工程实践说明【指导】  高配服务器限时抢购:企业级配置与回收服务一站式优惠方案  建站之星如何一键生成手机站?  小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建  简单实现Android文件上传  Python路径拼接规范_跨平台处理说明【指导】  制作网站哪家好,cc、.co、.cm哪个域名更适合做网站?  招贴海报怎么做,什么是海报招贴?  h5网站制作工具有哪些,h5页面制作工具有哪些?  如何确保西部建站助手FTP传输的安全性?  上海网站制作网站建设公司,建筑电工证网上查询系统入口?  详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析  如何获取免费开源的自助建站系统源码?  如何通过网站建站时间优化SEO与用户体验?  如何获取上海专业网站定制建站电话?  如何用免费手机建站系统零基础打造专业网站?  如何通过虚拟机搭建网站?详细步骤解析  如何选择高性价比服务器搭建个人网站?  大学网站设计制作软件有哪些,如何将网站制作成自己app?  如何在Golang中使用encoding/gob序列化对象_存储和传输数据  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  网站好制作吗知乎,网站开发好学吗?有什么技巧?  江苏网站制作公司有哪些,江苏书法考级官方网站?  制作网站的软件免费下载,免费制作app哪个平台好?  家庭服务器如何搭建个人网站?  新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?  c# 在ASP.NET Core中管理和取消后台任务  广州顶尖建站服务:企业官网建设与SEO优化一体化方案  微信小程序 input输入框控件详解及实例(多种示例)  建站之星2.7模板:企业网站建设与h5定制设计专题  在线制作视频的网站有哪些,电脑如何制作视频短片?  大型企业网站制作流程,做网站需要注册公司吗?  学校建站服务器如何选型才能满足性能需求?  焦点电影公司作品,电影焦点结局是什么? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。