下拉刷新之高仿京东头部快递小哥(1)
来源:互联网 发布:js获取video播放时间 编辑:程序博客网 时间:2024/05/02 02:24
先看主页面布局文件activity_main.xml--有一个SeekBar、还有一个缩放快递小哥的自定义控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.example.jingdong.FirstSetpView android:id="@+id/firstview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/seekbar" android:layout_centerInParent="true" /></RelativeLayout>
看主页面布局代码实现把MainActivity
package com.example.jingdong;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity {private SeekBar sb;private FirstSetpView mFirstStepView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sb = (SeekBar) findViewById(R.id.seekbar); mFirstStepView = (FirstSetpView) findViewById(R.id.firstview); //设置监听事件 sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {float currentProgress = (float)seekBar.getProgress()/(float)seekBar.getMax();mFirstStepView.setCurrentProgress(currentProgress);mFirstStepView.invalidate();}}); } }
接下来看自定义控件代码FirstSetpView
package com.example.jingdong;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class FirstSetpView extends View {private Bitmap people;private Bitmap peopleWithGoods;private float mCurrentProgress;private int mCurrentAlpha;private Paint mPaint;private int widthResult;private int heightResult;/** * 构造函数 * * @param context * @param attrs * @param defStyle */public FirstSetpView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public FirstSetpView(Context context, AttributeSet attrs) {super(context, attrs);init();}public FirstSetpView(Context context) {super(context);init();}/** * 1、获取包裹、快递小哥的图片资源 2、获取动画系类的最后一张图片(测量宽高) 3、初始化画笔,初始化透明度为0 */private void init() {people = BitmapFactory.decodeResource(getResources(),R.drawable.app_refresh_people_3);peopleWithGoods = BitmapFactory.decodeResource(getResources(),R.drawable.app_refresh_people_3);mPaint = new Paint();mPaint.setAlpha(0);}/** * 测量方法 * * @param widthMeasureSpec * @param heightMeasureSpec * MeasureSpec类中的三个Mode常量值的意义,其中UNSPECIFIED表示未指定,爹不会对儿子作任何的束缚, * 儿子想要多大都可以 * ;EXACTLY表示完全的,意为儿子多大爹心里有数,爹早已算好了;AT_MOST表示至多,爹已经为儿子设置好了一个最大限制 * ,儿子你不能比这个值大,不能再多了 * * MeasureSpec.EXACTLY是精确尺寸, * 当我们将控件的layout_width或layout_height指定为具体数值时如andorid * :layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 * * MeasureSpec.AT_MOST是最大尺寸, * 当控件的layout_width或layout_height指定为WRAP_CONTENT时 * ,控件大小一般随着控件的子空间或内容进行变化 * ,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));}// 测量宽度private int measureWidth(int widthMeasureSpec) {widthResult = 0;int size = MeasureSpec.getSize(widthMeasureSpec);int mode = MeasureSpec.getMode(widthMeasureSpec);if (MeasureSpec.EXACTLY == mode) {widthResult = size;} else {widthResult = peopleWithGoods.getWidth();if (MeasureSpec.AT_MOST == mode) {widthResult = Math.min(widthResult, size);}}return widthResult;}// 测量高度private int measureHeight(int heightMeasureSpec) {heightResult = 0;int size = MeasureSpec.getSize(heightMeasureSpec);int mode = MeasureSpec.getMode(heightMeasureSpec);if (MeasureSpec.EXACTLY == mode) {heightResult = size;} else {heightResult = peopleWithGoods.getHeight();if (MeasureSpec.AT_MOST == mode) {heightResult = Math.min(heightResult, size);}}return heightResult;}/** * 绘制方法 * * @param canvas */@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.scale(mCurrentProgress, mCurrentProgress);mPaint.setAlpha(mCurrentAlpha);canvas.drawBitmap(people, 0, 0, mPaint);}/** * 根据进度来对小哥和包裹进行缩放 * * @param currentProgress */public void setCurrentProgress(float currentProgress) {this.mCurrentProgress = currentProgress;this.mCurrentAlpha = (int) (currentProgress * 255);}}
0 0
- 下拉刷新之高仿京东头部快递小哥(1)
- 快递小哥泛型
- ListView下拉刷新(1)
- IOS之EGORefreshTableHeaderView下拉刷新(一)
- android下拉刷新之PullToRefreshListView(一)
- android下拉刷新之PullToRefreshListView(二)
- Andriod自定义View之(下拉刷新)
- 下拉刷新之 swipeRefreshLayout
- 那些年,你与快递小哥的爱恨情仇...
- iOS编程中的快递小哥-Responder Chain(响应链)
- 顺丰AI科学家:AI如何帮而不是取代快递小哥?
- 下拉刷新之ScrollView--PullToRefreshScrollView
- PushToRefresh之手动下拉刷新
- android下拉刷新之SwipeRefreshLayout
- android 之 下拉刷新PullToRefresh
- Android下拉刷新之ZrcListView
- Android之listview下拉刷新
- 自定义下拉刷新之仿AcFun下拉刷新
- HashMap与HashTable的区别、HashMap与HashSet的关系 t的关系
- javascript模块化编程一
- 原码, 反码, 补码 详解
- Time to sail!
- IDEA在DEBUG模式下慢的问题
- 下拉刷新之高仿京东头部快递小哥(1)
- HDU1051Wooden Sticks
- Nginx模块管理和进程管理
- javascript模块化编程二(AMD规范)
- 寻找旋转排序数组中的最小值
- NSTIMER 的保留环问题解决方法
- BIOS中断大全:
- 冒泡排序
- OpenJudge_P1776 木材加工