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