由于新的项目中在广告轮播用到了垂直滚动显示,所以也当学习,自己定义了一个垂直滚动的view以及监听
用到TextSwitcher和ViewFactory
TextSwitcher :字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
ViewFactory:顾名思义,view的工厂类,在实现方法makeView()方法中实现TextView的初始化
好了前期工作做好了上代码:
package xql.recycleviewdemo.view;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
import xql.recycleviewdemo.R;
import xql.recycleviewdemo.R.anim;
public class VerticalTextView extends TextSwitcher implements ViewFactory {
private ArrayList
private int mIndex = -1;
private final int START_TEXTVIEW = 1;
private final int STOP_TEXTIEW = 2;
private Context mContext;
// mInUp,mOutUp分别构成向下翻页的进出动画
private Rotate3dAnimation mInUp;
private Rotate3dAnimation mOutUp;
public VerticalTextView(Context context) {
super(context);
mContext = context;
init();
}
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
private void init() {
this.setFactory(this);
this.setInAnimation(getContext(), R.anim.vertical_in);
this.setOutAnimation(getContext(), R.anim.vertical_out);
// mInUp = createAnim(true, true);
// mOutUp = createAnim(false, true);
//
// setInAnimation(mInUp);// 当View显示时动画资源ID
// setOutAnimation(mOutUp);// 当View隐藏是动画资源ID。
}
private Rotate3dAnimation createAnim(boolean turnIn, boolean turnUp) {
Rotate3dAnimation rotation = new Rotate3dAnimation(turnIn, turnUp);
rotation.setDuration(1200);// 执行动画的时间
rotation.setFillAfter(false);// 是否保持动画完毕之后的状态
rotation.setInterpolator(new AccelerateInterpolator());// 设置加速模式
return rotation;
}
public void setResources(ArrayList
mList = list;
}
public Handler handler;
public void start() {
if (mList != null && mList.size() > 0) {
if (handler == null) {
initHandler();
}
handler.sendMessageDelayed(Message.obtain(handler, 1), 3000);
} else {
Toast.makeText(mContext, "请设置数据", 1).show();
}
}
public void stop() {
if (handler != null) {
handler.removeCallbacksAndMessages(null);// 删除Handler中的所有消息
handler = null;
}
}
public int getPosition() {
return mIndex;
}
public boolean onTouchEvent(android.view.MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("按下");
handler.removeCallbacksAndMessages(null);// 删除Handler中的所有消息
break;
case MotionEvent.ACTION_CANCEL:
System.out.println("事件取消");
handler.sendMessageDelayed(Message.obtain(handler, 1), 3000);
break;
case MotionEvent.ACTION_UP:
System.out.println("抬起");
handler.sendMessageDelayed(Message.obtain(handler, 1), 3000);
break;
default:
break;
}
return true;
};
@Override
public View makeView() {
TextView tv = new TextView(mContext);
tv.setTextColor(0x24aaff);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
tv.setSingleLine(true);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setEllipsize(TruncateAt.END);
tv.setTextColor(Color.RED);
tv.setTextSize(20);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listenre != null) {
listenre.onClick(mIndex);
}
}
});
return tv;
}
private void initHandler() {
if (handler == null) {
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case START_TEXTVIEW:
updateTextSwitcher();
handler.sendMessageDelayed(Message.obtain(handler, 1), 3000);
break;
case STOP_TEXTIEW:
handler.removeCallbacksAndMessages(null);
break;
}
}
};
}
}
private void updateTextSwitcher() {
if (mList != null && mList.size() > 0) {
if (mIndex > this.mList.size() - 2) {
mIndex = -1;
}
this.setText(mList.get(++mIndex));
}
};
class Rotate3dAnimation extends Animation {
private float mCenterX;
private float mCenterY;
private final boolean mTurnIn;
private final boolean mTurnUp;
private Camera mCamera;
public Rotate3dAnimation(boolean turnIn, boolean turnUp) {
mTurnIn = turnIn;
mTurnUp = turnUp;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
mCenterY = getHeight();
mCenterX = getWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final int derection = mTurnUp ? 1 : -1;
final Matrix matrix = t.getMatrix();
camera.save();
if (mTurnIn) {
camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f);
} else {
camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f);
}
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
private OnClickListner listenre;
public interface OnClickListner {
void onClick(int position);
}
public void setOnClickListener(OnClickListner listener) {
this.listenre = listener;
}
}
至于转换动画则是用了两种,一种补间动画的translate 动画
vertical_out.xml
android:fromYDelta="-100%p"
android:toYDelta="0%p" />
vertical_in.xml
android:fromYDelta="0%p"
android:toYDelta="100%p"
/>
以及网上当下来的代码,已在代码中实现
实现监听是通过textView的监听回调的,如果用该控件的监听,会失效不知道是不是方法不对还是事件分发处理问题,如果有知道的希望告诉我,谢谢