android拖动view
A. android viewdraghelper怎么点击
要理解ViewDragHelper,我们需要掌握以下几点:
ViewDragHelper.Callback是连接ViewDragHelper与view之间的桥梁;
ViewDragHelper的实例是通过静态工厂方法创建的;
ViewDragHelper可以检测到是否触及到边缘;
ViewDragHelper并不是直接作用于要被拖动的View,而是使其控制的视图容器中的子View可以被拖动,如果要指定某个子view的行为,需要在Callback中实现;
ViewDragHelper的本质其实是分析onInterceptTouchEvent和onTouchEvent的MotionEvent参数,然后根据分析的结果去改变一个容器中被拖动子View的位置。
拖动行为处理
在DragHelperCallback的回调方法中有很多的方法可以检测View的事件,如常见的clampViewPositionHorizontal、clampViewPositionVertical,并且clampViewPositionHorizontal 和 clampViewPositionVertical必须要重写,因为默认它返回的是0。
来看clampViewPositionHorizontal的处理。
在DragHelperCallback中实现clampViewPositionHorizontal方法, 并且返回一个适当的数值就能实现横向拖动效果。@Override
(Viewchild,intleft,intdx){
Log.d("DragLayout","clampViewPositionHorizontal"+left+","+dx);
finalintleftBound=getPaddingLeft();
finalintrightBound=getWidth()-mDragView.getWidth();
finalintnewLeft=Math.min(Math.max(left,leftBound),rightBound);
returnnewLeft;
}
B. android webview控制页面拖动
在main.xml中,设置线性布局linearlayout,除webview外,其它控件widget设置隐藏
android:visibility="gone" 。就可以了。
C. android文字横向滚动的自定义view
TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine=”true” 2.设置可滚到,或显示样式
TextView实现文字滚动需要以下几个要点:
1.文字长度长于可显示范围:android:singleLine=”true”
2.设置可滚到,或显示样式:android:ellipsize=”marquee”
3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。
自定义一个
AlwaysMarqueeTextView 类
public class AlwaysMarqueeTextView extends TextView { public AlwaysMarqueeTextView(Context context) { super(context); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; }
在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。
<com.examples.AlwaysMarqueeTextView android:id=“@+id/AMTV1″ android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:lines=“1″ android:focusable=“true” android:focusableInTouchMode=“true” android:scrollHorizontally=“true” android:marqueeRepeatLimit=“marquee_forever” android:ellipsize=“marquee” android:background=“@android:color/transparent” />
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。
组合View的问题:
< LinearLayout xmlns:android =“http://schemas.android.com/apk/res/android” android:orientation =“vertical” android:gravity =“center_vertical” android:background =“@drawable/f_background” android:layout_width =“fill_parent” android:focusable =“true” android:layout_height =“50px” > < TextView android:id =“@+id/info_text” android:focusable =“true” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:text =“test marquee .. “ android:textColor =“@color/black” android:singleLine =“true” android:ellipsize =“marquee” android:marqueeRepeatLimit =“3″ android:textSize =“18sp” /> < TextView android:id =“@+id/date_text” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:layout_gravity =“bottom” android:textColor =“@color/gray” android:text =“2010/05/28″ android:textSize =“12sp” /> </ LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的
D. 如何禁止android webview 向下拖拽
调用webview的setOverScrollMode方法 参数传OVER_SCROLL_NEVER
E. 请问android编程中,textview里面放了很多文字,怎样拖动textview让下面的内容显示出来。
TextView本身不具备这样的功能。
你可以把你的TextView放到一个ScrollView里面。固定ScrollView的宽(layout_width)和高(layout_height);把TextView的layout_width设为fill_parent,layout_height设为wrap_content.
这样就可以实现相应的效果了。
希望能帮到你!
F. 如何从屏幕底部向上滑出一个view-Android开发问答
从屏幕底部向上滑出一个view的方式,主要是使用TranslateAnimation,这个类,可以绑定一个控件,在y轴方向,滑出一段高度,如下代码:
java">packagecom.txlong;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.Button;
importandroid.widget.TextView;
{
privateAnimationmyAnimation_Translate;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalTextViewtv=(TextView)findViewById(R.id.tv);
Buttonbtn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
tv.setVisibility(View.VISIBLE);
myAnimation_Translate=newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1,
Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0);
myAnimation_Translate.setDuration(1000);
myAnimation_Translate.setInterpolator(AnimationUtils
.loadInterpolator(AndroidAnimationActivity.this,
android.R.anim.accelerate_decelerate_interpolator));
tv.startAnimation(myAnimation_Translate);
}
});
}
}
G. android 在view上的指定位置画了一个图片,怎么实现这个图片的拖拽
你先继承 VIEW . 然后重写以下函数 就可以实现拖拽的功能了.
至于要显示图片,你可以直接在 onDraw里面描绘出来就好
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
//layoutParams.rightMargin = -250;
//layoutParams.bottomMargin = -250;
HanoiItem.this.setLayoutParams(layoutParams); //自己继承VIEW的this
break;
}
invalidate();
return true;
}
至于位置,你就可以直接new VIEW 之后直接 用 VIEW.set**实现的啊
H. android viewpager 拖拽翻页时报数组下标越界是什么情况
就是你没有限制它的最大的数目啊,这样肯定超出了它的范围,当然就不行了会报错啊
I. android 如何平移view
需要手势监听吧,控件移动一般都是相对布局,重写该控件的onTouch方法,在该方法中判断,是down状态记录按下的位置,move状态设置该view的margin或者在放手状态up中设置。
J. android webview如何拖动网页中的可拖动元素 如div
jsp +android+webview