欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-04-13 09:16:01|栏目: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中的代码?C就是一个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中Rxjava实现三级缓存的两种方式

栏    目:Android代码

下一篇:Android编程实现滑动按钮功能详解

本文标题:Android自定义时间轴的实现过程

本文地址:http://www.codeinn.net/misctech/100658.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有