先看主页面布局文件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() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public 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给出了父控件允许的最大尺寸。
*/
@Override
protected 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
*/
@Override
protected 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);
}
}