android 数字上下滚动怎么实现上下循环时间数字

&nbsp&#8250&nbsp&nbsp&#8250&nbsp
Android自定义控件实战―滚动选择器PickerView
转载自: &手机里设置闹钟需要选择时间,那个选择时间的控件就是滚动选择器,前几天用手机刷了MIUI,发现自带的那个时间选择器效果挺好看的,于是就自己仿写了一个,权当练手。先来看效果:& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &效果还行吧?实现思路就是自定义一个PickerView,单独滚动的是一个
PickerView,显然上图中有分和秒的选择所以在布局里用了两个PickerView。由于这里不涉及到text的点击事件,所以只需要继承
View就行了,直接把text用canvas画上去。PickerView的实现的主要难点:难点1:& & & &
字体随距离的渐变。可以看到,text随离中心位置的距离变化而变化,这里变化的是透明度alpha和字体大小TexSize,这两个值我都设置了Max
和Min值,通过其与中心点的距离计算scale。我用的是变化曲线是抛物线scale=1-ax^2(x&=Height/4),scale =
0(x&Height/4),a=(4/Height)^2。x就是距离View中心的偏移量。用图片表示如下:难点2:& & &text的居中。绘制text的时候不仅要使其在x方向上居中,还要在y方向上居中,在x方向上比较简单,设置Paint的Align为Align.CENTER就行了,但是y方向上很蛋疼,需要计算text的baseline。难点3:& & 循环滚动。为了解决循环滚动的问题我把存放text的List从中间往上下摊开,通过不断地moveHeadToTail和moveTailToHead使选中的text始终是list的中间position的值。&&& & &以上就是几个难点,了解了之后可以来看PickerView的代码了:package&com.jingchen.
import&java.util.ArrayL
import&java.util.L
import&java.util.T
import&java.util.TimerT
import&android.content.C
import&android.graphics.C
import&android.graphics.P
import&android.graphics.Paint.A
import&android.graphics.Paint.FontMetricsI
import&android.graphics.Paint.S
import&android.os.H
import&android.os.M
import&android.util.AttributeS
import&android.view.MotionE
import&android.view.V
&*&滚动选择器
&*&@author&chenjing
public&class&PickerView&extends&View
&&&&public&static&final&String&TAG&=&&PickerView&;
&&&&&*&text之间间距和minTextSize之比
&&&&public&static&final&float&MARGIN_ALPHA&=&2.8f;
&&&&&*&自动回滚到中间的速度
&&&&public&static&final&float&SPEED&=&2;
&&&&private&List&String&&mDataL
&&&&&*&选中的位置,这个位置是mDataList的中心位置,一直不变
&&&&private&int&mCurrentS
&&&&private&Paint&mP
&&&&private&float&mMaxTextSize&=&80;
&&&&private&float&mMinTextSize&=&40;
&&&&private&float&mMaxTextAlpha&=&255;
&&&&private&float&mMinTextAlpha&=&120;
&&&&private&int&mColorText&=&0x333333;
&&&&private&int&mViewH
&&&&private&int&mViewW
&&&&private&float&mLastDownY;
&&&&&*&滑动的距离
&&&&private&float&mMoveLen&=&0;
&&&&private&boolean&isInit&=&
&&&&private&onSelectListener&mSelectL
&&&&private&Timer&
&&&&private&MyTimerTask&mT
&&&&Handler&updateHandler&=&new&Handler()
&&&&&&&&@Override
&&&&&&&&public&void&handleMessage(Message&msg)
&&&&&&&&&&&&if&(Math.abs(mMoveLen)&&&SPEED)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&mMoveLen&=&0;
&&&&&&&&&&&&&&&&if&(mTask&!=&null)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&mTask.cancel();
&&&&&&&&&&&&&&&&&&&&mTask&=&
&&&&&&&&&&&&&&&&&&&&performSelect();
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}&else
&&&&&&&&&&&&&&&&//&这里mMoveLen&/&Math.abs(mMoveLen)是为了保有mMoveLen的正负号,以实现上滚或下滚
&&&&&&&&&&&&&&&&mMoveLen&=&mMoveLen&-&mMoveLen&/&Math.abs(mMoveLen)&*&SPEED;
&&&&&&&&&&&&invalidate();
&&&&public&PickerView(Context&context)
&&&&&&&&super(context);
&&&&&&&&init();
&&&&public&PickerView(Context&context,&AttributeSet&attrs)
&&&&&&&&super(context,&attrs);
&&&&&&&&init();
&&&&public&void&setOnSelectListener(onSelectListener&listener)
&&&&&&&&mSelectListener&=&
&&&&private&void&performSelect()
&&&&&&&&if&(mSelectListener&!=&null)
&&&&&&&&&&&&mSelectListener.onSelect(mDataList.get(mCurrentSelected));
&&&&public&void&setData(List&String&&datas)
&&&&&&&&mDataList&=&
&&&&&&&&mCurrentSelected&=&datas.size()&/&2;
&&&&&&&&invalidate();
&&&&public&void&setSelected(int&selected)
&&&&&&&&mCurrentSelected&=&
&&&&private&void&moveHeadToTail()
&&&&&&&&String&head&=&mDataList.get(0);
&&&&&&&&mDataList.remove(0);
&&&&&&&&mDataList.add(head);
&&&&private&void&moveTailToHead()
&&&&&&&&String&tail&=&mDataList.get(mDataList.size()&-&1);
&&&&&&&&mDataList.remove(mDataList.size()&-&1);
&&&&&&&&mDataList.add(0,&tail);
&&&&@Override
&&&&protected&void&onMeasure(int&widthMeasureSpec,&int&heightMeasureSpec)
&&&&&&&&super.onMeasure(widthMeasureSpec,&heightMeasureSpec);
&&&&&&&&mViewHeight&=&getMeasuredHeight();
&&&&&&&&mViewWidth&=&getMeasuredWidth();
&&&&&&&&//&按照View的高度计算字体大小
&&&&&&&&mMaxTextSize&=&mViewHeight&/&4.0f;
&&&&&&&&mMinTextSize&=&mMaxTextSize&/&2f;
&&&&&&&&isInit&=&
&&&&&&&&invalidate();
&&&&private&void&init()
&&&&&&&&timer&=&new&Timer();
&&&&&&&&mDataList&=&new&ArrayList&String&();
&&&&&&&&mPaint&=&new&Paint(Paint.ANTI_ALIAS_FLAG);
&&&&&&&&mPaint.setStyle(Style.FILL);
&&&&&&&&mPaint.setTextAlign(Align.CENTER);
&&&&&&&&mPaint.setColor(mColorText);
&&&&@Override
&&&&protected&void&onDraw(Canvas&canvas)
&&&&&&&&super.onDraw(canvas);
&&&&&&&&//&根据index绘制view
&&&&&&&&if&(isInit)
&&&&&&&&&&&&drawData(canvas);
&&&&private&void&drawData(Canvas&canvas)
&&&&&&&&//&先绘制选中的text再往上往下绘制其余的text
&&&&&&&&float&scale&=&parabola(mViewHeight&/&4.0f,&mMoveLen);
&&&&&&&&float&size&=&(mMaxTextSize&-&mMinTextSize)&*&scale&+&mMinTextS
&&&&&&&&mPaint.setTextSize(size);
&&&&&&&&mPaint.setAlpha((int)&((mMaxTextAlpha&-&mMinTextAlpha)&*&scale&+&mMinTextAlpha));
&&&&&&&&//&text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标
&&&&&&&&float&x&=&(float)&(mViewWidth&/&2.0);
&&&&&&&&float&y&=&(float)&(mViewHeight&/&2.0&+&mMoveLen);
&&&&&&&&FontMetricsInt&fmi&=&mPaint.getFontMetricsInt();
&&&&&&&&float&baseline&=&(float)&(y&-&(fmi.bottom&/&2.0&+&fmi.top&/&2.0));
&&&&&&&&canvas.drawText(mDataList.get(mCurrentSelected),&x,&baseline,&mPaint);
&&&&&&&&//&绘制上方data
&&&&&&&&for&(int&i&=&1;&(mCurrentSelected&-&i)&&=&0;&i++)
&&&&&&&&&&&&drawOtherText(canvas,&i,&-1);
&&&&&&&&//&绘制下方data
&&&&&&&&for&(int&i&=&1;&(mCurrentSelected&+&i)&&&mDataList.size();&i++)
&&&&&&&&&&&&drawOtherText(canvas,&i,&1);
&&&&&*&@param&canvas
&&&&&*&@param&position
&&&&&*&&&&&&&&&&&&距离mCurrentSelected的差值
&&&&&*&@param&type
&&&&&*&&&&&&&&&&&&1表示向下绘制,-1表示向上绘制
&&&&private&void&drawOtherText(Canvas&canvas,&int&position,&int&type)
&&&&&&&&float&d&=&(float)&(MARGIN_ALPHA&*&mMinTextSize&*&position&+&type
&&&&&&&&&&&&&&&&*&mMoveLen);
&&&&&&&&float&scale&=&parabola(mViewHeight&/&4.0f,&d);
&&&&&&&&float&size&=&(mMaxTextSize&-&mMinTextSize)&*&scale&+&mMinTextS
&&&&&&&&mPaint.setTextSize(size);
&&&&&&&&mPaint.setAlpha((int)&((mMaxTextAlpha&-&mMinTextAlpha)&*&scale&+&mMinTextAlpha));
&&&&&&&&float&y&=&(float)&(mViewHeight&/&2.0&+&type&*&d);
&&&&&&&&FontMetricsInt&fmi&=&mPaint.getFontMetricsInt();
&&&&&&&&float&baseline&=&(float)&(y&-&(fmi.bottom&/&2.0&+&fmi.top&/&2.0));
&&&&&&&&canvas.drawText(mDataList.get(mCurrentSelected&+&type&*&position),
&&&&&&&&&&&&&&&&(float)&(mViewWidth&/&2.0),&baseline,&mPaint);
&&&&&*&抛物线
&&&&&*&@param&zero
&&&&&*&&&&&&&&&&&&零点坐标
&&&&&*&@param&x
&&&&&*&&&&&&&&&&&&偏移量
&&&&&*&@return&scale
&&&&private&float&parabola(float&zero,&float&x)
&&&&&&&&float&f&=&(float)&(1&-&Math.pow(x&/&zero,&2));
&&&&&&&&return&f&&&0&?&0&:&f;
&&&&@Override
&&&&public&boolean&onTouchEvent(MotionEvent&event)
&&&&&&&&switch&(event.getActionMasked())
&&&&&&&&case&MotionEvent.ACTION_DOWN:
&&&&&&&&&&&&doDown(event);
&&&&&&&&&&&&
&&&&&&&&case&MotionEvent.ACTION_MOVE:
&&&&&&&&&&&&doMove(event);
&&&&&&&&&&&&
&&&&&&&&case&MotionEvent.ACTION_UP:
&&&&&&&&&&&&doUp(event);
&&&&&&&&&&&&
&&&&&&&&return&
&&&&private&void&doDown(MotionEvent&event)
&&&&&&&&if&(mTask&!=&null)
&&&&&&&&&&&&mTask.cancel();
&&&&&&&&&&&&mTask&=&
&&&&&&&&mLastDownY&=&event.getY();
&&&&private&void&doMove(MotionEvent&event)
&&&&&&&&mMoveLen&+=&(event.getY()&-&mLastDownY);
&&&&&&&&if&(mMoveLen&&&MARGIN_ALPHA&*&mMinTextSize&/&2)
&&&&&&&&&&&&//&往下滑超过离开距离
&&&&&&&&&&&&moveTailToHead();
&&&&&&&&&&&&mMoveLen&=&mMoveLen&-&MARGIN_ALPHA&*&mMinTextS
&&&&&&&&}&else&if&(mMoveLen&&&-MARGIN_ALPHA&*&mMinTextSize&/&2)
&&&&&&&&&&&&//&往上滑超过离开距离
&&&&&&&&&&&&moveHeadToTail();
&&&&&&&&&&&&mMoveLen&=&mMoveLen&+&MARGIN_ALPHA&*&mMinTextS
&&&&&&&&mLastDownY&=&event.getY();
&&&&&&&&invalidate();
&&&&private&void&doUp(MotionEvent&event)
&&&&&&&&//&抬起手后mCurrentSelected的位置由当前位置move到中间选中位置
&&&&&&&&if&(Math.abs(mMoveLen)&&&0.0001)
&&&&&&&&&&&&mMoveLen&=&0;
&&&&&&&&&&&&
&&&&&&&&if&(mTask&!=&null)
&&&&&&&&&&&&mTask.cancel();
&&&&&&&&&&&&mTask&=&
&&&&&&&&mTask&=&new&MyTimerTask(updateHandler);
&&&&&&&&timer.schedule(mTask,&0,&10);
&&&&class&MyTimerTask&extends&TimerTask
&&&&&&&&Handler&
&&&&&&&&public&MyTimerTask(Handler&handler)
&&&&&&&&&&&&this.handler&=&
&&&&&&&&@Override
&&&&&&&&public&void&run()
&&&&&&&&&&&&handler.sendMessage(handler.obtainMessage());
&&&&public&interface&onSelectListener
&&&&&&&&void&onSelect(String&text);
}代码里的注释都写的很清楚了。接下来,我们就用写好的PickerView实现文章开头的图片效果吧~首先看MainActivity的布局:&RelativeLayout&xmlns:android=&/apk/res/android&
&&&&android:layout_width=&match_parent&
&&&&android:layout_height=&match_parent&
&&&&android:background=&#000000&&&
&&&&&RelativeLayout
&&&&&&&&android:layout_width=&wrap_content&
&&&&&&&&android:layout_height=&wrap_content&
&&&&&&&&android:layout_centerInParent=&true&
&&&&&&&&android:background=&#ffffff&&&
&&&&&&&&&com.jingchen.timerpicker.PickerView
&&&&&&&&&&&&android:id=&@+id/minute_pv&
&&&&&&&&&&&&android:layout_width=&80dp&
&&&&&&&&&&&&android:layout_height=&160dp&&/&
&&&&&&&&&TextView
&&&&&&&&&&&&android:id=&@+id/minute_tv&
&&&&&&&&&&&&android:layout_width=&wrap_content&
&&&&&&&&&&&&android:layout_height=&wrap_content&
&&&&&&&&&&&&android:layout_centerVertical=&true&
&&&&&&&&&&&&android:layout_toRightOf=&@id/minute_pv&
&&&&&&&&&&&&android:text=&分&
&&&&&&&&&&&&android:textColor=&#ffaa33&
&&&&&&&&&&&&android:textSize=&26sp&
&&&&&&&&&&&&android:textStyle=&bold&&/&
&&&&&&&&&com.jingchen.timerpicker.PickerView
&&&&&&&&&&&&android:id=&@+id/second_pv&
&&&&&&&&&&&&android:layout_width=&80dp&
&&&&&&&&&&&&android:layout_height=&160dp&
&&&&&&&&&&&&android:layout_toRightOf=&@id/minute_tv&&/&
&&&&&&&&&TextView
&&&&&&&&&&&&android:id=&@+id/second_tv&
&&&&&&&&&&&&android:layout_width=&wrap_content&
&&&&&&&&&&&&android:layout_height=&wrap_content&
&&&&&&&&&&&&android:layout_centerVertical=&true&
&&&&&&&&&&&&android:layout_toRightOf=&@id/second_pv&
&&&&&&&&&&&&android:text=&秒&
&&&&&&&&&&&&android:textColor=&#ffaa33&
&&&&&&&&&&&&android:textSize=&26sp&
&&&&&&&&&&&&android:textStyle=&bold&&/&
&&&&&/RelativeLayout&
&/RelativeLayout&两个PickerView两个TextView,很简单。下面是MainActivity的代码:package&com.jingchen.
import&java.util.ArrayL
import&java.util.L
import&com.jingchen.timerpicker.PickerView.onSelectL
import&android.app.A
import&android.os.B
import&android.view.M
import&android.widget.TextV
import&android.widget.T
public&class&MainActivity&extends&Activity
&&&&PickerView&minute_
&&&&PickerView&second_
&&&&@Override
&&&&protected&void&onCreate(Bundle&savedInstanceState)
&&&&&&&&super.onCreate(savedInstanceState);
&&&&&&&&setContentView(R.layout.activity_main);
&&&&&&&&minute_pv&=&(PickerView)&findViewById(R.id.minute_pv);
&&&&&&&&second_pv&=&(PickerView)&findViewById(R.id.second_pv);
&&&&&&&&List&String&&data&=&new&ArrayList&String&();
&&&&&&&&List&String&&seconds&=&new&ArrayList&String&();
&&&&&&&&for&(int&i&=&0;&i&&&10;&i++)
&&&&&&&&&&&&data.add(&0&&+&i);
&&&&&&&&for&(int&i&=&0;&i&&&60;&i++)
&&&&&&&&&&&&seconds.add(i&&&10&?&&0&&+&i&:&&&&+&i);
&&&&&&&&minute_pv.setData(data);
&&&&&&&&minute_pv.setOnSelectListener(new&onSelectListener()
&&&&&&&&&&&&@Override
&&&&&&&&&&&&public&void&onSelect(String&text)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&Toast.makeText(MainActivity.this,&&选择了&&&+&text&+&&&分&,
&&&&&&&&&&&&&&&&&&&&&&&&Toast.LENGTH_SHORT).show();
&&&&&&&&&&&&}
&&&&&&&&});
&&&&&&&&second_pv.setData(seconds);
&&&&&&&&second_pv.setOnSelectListener(new&onSelectListener()
&&&&&&&&&&&&@Override
&&&&&&&&&&&&public&void&onSelect(String&text)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&Toast.makeText(MainActivity.this,&&选择了&&&+&text&+&&&秒&,
&&&&&&&&&&&&&&&&&&&&&&&&Toast.LENGTH_SHORT).show();
&&&&&&&&&&&&}
&&&&&&&&});
&&&&@Override
&&&&public&boolean&onCreateOptionsMenu(Menu&menu)
&&&&&&&&getMenuInflater().inflate(R.menu.main,&menu);
&&&&&&&&return&
}OK了,自定义自己的TimerPicker就是这么简单~.github地址:&
上一篇: 蘑菇街是一个开源的im系统,包含了客户端和服务端源码。下面这篇文章是期官方发布的技术博客。更多的相关文章请到官方博客查看,这里相当于一个引入。 转自 http://mogu.io/android-im-design ###1. 架构总览 ###2. 模块介绍 ####2.1 协议封装与任务流程 ###
下一篇: 这篇文章其实原文叫 《老罗的Android之旅》导读PPT 是罗升阳的博客,对于在应用层已经开发了一段时间的人来说,读完之后会有很多体会,对初学者来说意义不大。 虽然好几个月没更新博客了,但是老罗一直有在准备可以分享的东西的。除了早前在微博分享 Android求教,淘宝头条,这个上下自动滚动是怎么实现的?-Android开发问答-eoe 移动开发者论坛 -
Powered by Discuz!
后使用快捷导航没有帐号?
只需一步,快速开始
<a href="javaAndroid 用ScrollView和HorizontalScrollView同时实现上下、左右滚动 - 推酷
Android 用ScrollView和HorizontalScrollView同时实现上下、左右滚动
直接上代码,.xml布局文件
&?xml version=&1.0& encoding=&utf-8&?&
&ScrollView xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
&HorizontalScrollView
android:orientation=&horizontal&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
&LinearLayout android:orientation=&vertical& android:layout_width=&fill_parent& android:layout_height=&fill_parent&&
&LinearLayout android:orientation=&horizontal&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第1列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第2列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第3列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第4列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第5列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第6列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第7列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第8列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第9列 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第10列 | &/&
&/LinearLayout&
&LinearLayout android:orientation=&vertical&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第1行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第2行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第3行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第4行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第5行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第6行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第7行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第8行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第9行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第10行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第11行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第12行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第13行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第14行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第15行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第16行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第17行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第18行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第19行 | &/&
&TextView android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:padding=&5px&
android:text=&第20行 | &/&
&/LinearLayout&
&/LinearLayout&
&/HorizontalScrollView&
&/ScrollView&
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致> 博客详情
摘要: Android Property Animation 动画有很多大神写过,都非常好,这里只通过ObjectAnimator /ValueAnimator实现上下左右浮动效果。
- 欢迎关注http://quanke.name/
- 转载请注明出处,谢谢
`Android Property Animation` 动画有很多大神写过,都非常好,这里只通过ObjectAnimator /ValueAnimator实现上下左右浮动效果。
很简单看代码就好
private void floatAnim(View view,int delay){
List&Animator& animators = new ArrayList&&();
ObjectAnimator translationXAnim = ObjectAnimator.ofFloat(view, "translationX", -6.0f,6.0f,-6.0f);
translationXAnim.setDuration(1500);
translationXAnim.setRepeatCount(ValueAnimator.INFINITE);//无限循环
translationXAnim.setRepeatMode(ValueAnimator.INFINITE);//
translationXAnim.start();
animators.add(translationXAnim);
ObjectAnimator translationYAnim = ObjectAnimator.ofFloat(view, "translationY", -3.0f,3.0f,-3.0f);
translationYAnim.setDuration(1000);
translationYAnim.setRepeatCount(ValueAnimator.INFINITE);
translationYAnim.setRepeatMode(ValueAnimator.INFINITE);
translationYAnim.start();
animators.add(translationYAnim);
AnimatorSet btnSexAnimatorSet = new AnimatorSet();
btnSexAnimatorSet.playTogether(animators);
btnSexAnimatorSet.setStartDelay(delay);
btnSexAnimatorSet.start();
开始设置`setRepeatMode(ValueAnimator.INFINITE);`方法没有效果,只有设置`setRepeatCount(ValueAnimator.INFINITE)`才可以。
人打赏支持
开源项目作者
领取时间:
作为一个开源项目作者,是时候站出来拯救世界了!
领取条件:开源项目被开源中国收录的开发者可领取
码字总数 25227
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥Android实现循环平移动画示例
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android实现循环平移动画示例,本文讲解实现用一张背景图做循环从左往右平移动画,需要的朋友可以参考下
实现用一张背景图做循环从左往右平移动画。
1、实现两个animation xml文件,一个起始位置在-100%p ,一个在0%p。设置repeat属性为循环,重复。
&&#63;xml version="1.0" encoding="utf-8"&#63;&
&set xmlns:android="/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"&
&&& &translate android:fromXDelta="0%p" android:toXDelta="100%p"
&&&&&&& android:repeatMode="restart"
&&&&&&& android:interpolator="@android:anim/linear_interpolator"
&&&&&&& android:repeatCount="infinite"
&&&&&&& android:duration="30000" /&
&&#63;xml version="1.0" encoding="utf-8"&#63;&
&set xmlns:android="/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"&
&&& &translate android:fromXDelta="-100%p" android:toXDelta="0%p"
&&&&&&& android:repeatMode="restart"
&&&&&&& android:interpolator="@android:anim/linear_interpolator"
&&&&&&& android:repeatCount="infinite"
&&&&&&& android:duration="30000" /&
2、在view的layout里面放两个一样的view做背景,view的动画分别对应上面那两个animation。
&&&&&&& &ImageView
&&&&&&&&&&&& android:id="@+id/animation_top_left"
&&&&&&&&&&&& android:layout_width="wrap_content"
&&&&&&&&&&&& android:layout_height="wrap_content"
&&&&&&&&&&&& android:contentDescription="@string/logo"
&&&&&&&&&&&& android:src="@drawable/home_animation_bg" /&
&&&&&&&& &ImageView
&&&&&&&&&&&& android:id="@+id/animation_top_right"& android:layout_width="wrap_content"
&&&&&&&&&&&& android:layout_height="wrap_content"
&&&&&&&&&&&& android:contentDescription="@string/logo"
&&&&&&&&&&&& android:src="@drawable/home_animation_bg" /&
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.home_animation);
ImageView animationTopRightView = (ImageView)this.findViewById(R.id.animation_top_right);
animationTopRightView.startAnimation(anim);
Animation anim2 = AnimationUtils.loadAnimation(mContext, R.anim.home_animation2);
ImageView animationTopLeftView = (ImageView)this.findViewById(R.id.animation_top_left);
animationTopLeftView.startAnimation(anim2);
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 android 上下抽屉实现 的文章

 

随机推荐