android控件平移后可点击,Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)...

MainActivity例如以下:

package cc.cn;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.app.Activity;

/**

* Demo描写叙述:

* Scroller使用演示样例——让控件平移划过屏幕

*

* 參考资料:

* http://blog.youkuaiyun.com/c_weibin/article/details/7438323

* Thank you very much

*

* 注意事项:

* 1 在布局中将cc.cn.LinearLayoutSubClass的控件的宽度设置为"fill_parent"

* 便于观察滑动的效果

*/

public class MainActivity extends Activity {

private Button mButton;

private LinearLayoutSubClass mLinearLayoutSubClass;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

init();

}

private void init(){

mLinearLayoutSubClass=(LinearLayoutSubClass) findViewById(R.id.linearLayoutSubClass);

mButton=(Button) findViewById(R.id.button);

mButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

mLinearLayoutSubClass.beginScroll();

}

});

}

}

LinearLayoutSubClass例如以下:

package cc.cn;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.LinearLayout;

import android.widget.Scroller;

/**

* API凝视:

*

* 1 //第一,二个參数起始位置;第三,四个滚动的偏移量;第五个參数持续时间

* startScroll(int startX, int startY, int dx, int dy, int duration)

*

* 2 //在startScroll()方法运行过程中即在duration时间内computeScrollOffset()

* 方法会一直返回true,但当动画运行完毕后会返回返加false.

* computeScrollOffset()

*

* 3 当运行ontouch()或invalidate()或postInvalidate()均会调用该方法

* computeScroll()

*

*/

public class LinearLayoutSubClass extends LinearLayout {

private Scroller mScroller;

private boolean flag=true;

public LinearLayoutSubClass(Context context) {

super(context);

}

public LinearLayoutSubClass(Context context, AttributeSet attrs) {

super(context, attrs);

//也可採用该构造方法传入一个interpolator

//mScroller=new Scroller(context, interpolator);

mScroller=new Scroller(context);

}

@Override

public void computeScroll() {

super.computeScroll();

if(mScroller.computeScrollOffset()){

scrollTo(mScroller.getCurrX(), 0);

//使其再次调用computeScroll()直至滑动结束,即不满足if条件

postInvalidate();

}

}

public void beginScroll(){

if (flag) {

mScroller.startScroll(0, 0, -2500, 0, 2500);

flag = false;

} else {

mScroller.startScroll(0, 0, 0, 0, 1500);

flag = true;

}

//调用invalidate();使其调用computeScroll()

invalidate();

}

}

main.xml例如以下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="点击后滑动" />

android:id="@+id/linearLayoutSubClass"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ff1100"

android:text="測试Scroller" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值