頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果_第1頁(yè)
頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果_第2頁(yè)
頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果_第3頁(yè)
頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果_第4頁(yè)
頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果_第5頁(yè)
已閱讀5頁(yè),還剩72頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、頂峰APP開(kāi)發(fā)Android系列初學(xué)者之左右拖動(dòng)效果FlingGalleryActivity.java 001import android.app.Activity;002 003import android.os.Bundle;004 005 006 007import android.content.Context;008 009import android.graphics.Color;010 011import android.util.Log;012 013import android.view.Gravity;014 015import android.view.MotionEve

2、nt;016 017import android.view.View;018 019import android.view.ViewGroup;020 021import android.view.View.OnClickListener;022 023import android.widget.ArrayAdapter;024 025import android.widget.Button;026 027import android.widget.CheckBox;028 029import android.widget.EditText;030 031import android.widg

3、et.LinearLayout;032 033import android.widget.TableLayout;034 035import android.widget.TextView;036 037 038 039public class FlingGalleryActivity extends Activity040 041042 043 private final int color_red = Color.argb(100, 200, 0, 0);044 045 private final int color_green = Color.argb(100, 0, 200, 0);0

4、46 047 private final int color_blue = Color.argb(100, 0, 0, 200);048 049 private final int color_yellow = Color.argb(100, 200, 200, 0);050 051 private final int color_purple = Color.argb(100, 200, 0, 200);052 053 054 055 private final String<span></span> mLabelArray = "View1",

5、"View2","View3", "View4", "View5"056 057 private final int<span></span> mColorArray = color_red, color_green, color_blue, color_yellow, color_purple;058 059 060 061 private FlingGallery mGallery;062 063 private CheckBox mCheckBox;064 065 066 067 /

6、Note: The following handler is critical to correct function of068 069 / the FlingGallery class. This enables the FlingGallery class to070 071 / detect when the motion event has ended by finger being lifted072 073 074 075 Override076 077 public boolean onTouchEvent(MotionEvent event)078 079 080 081 r

7、eturn mGallery.onGalleryTouchEvent(event);082 083 084 085 086 087 public void onCreate(Bundle savedInstanceState)088 089 090 091 super.onCreate(savedInstanceState);092 093 094 095 mGallery = new FlingGallery(this);096 097 mGallery.setPaddingWidth(5);098 099 mGallery.setAdapter(new ArrayAdapter<St

8、ring>(getApplicationContext(), android.R.layout.simple_list_item_1, mLabelArray)100 101 102 103 Override104 105 public View getView(int position, View convertView, ViewGroup parent)106 107 108 109 Log.d("111", "count="+position);110 111/ if (convertView != null && conv

9、ertView instanceof GalleryViewItem)112 113/ 114 115/ GalleryViewItem galleryView = (GalleryViewItem) convertView;116 117/118 119/ galleryView.mEdit1.setText("");120 121/ galleryView.mText1.setText(mLabelArray<span></span>position);122 123/ galleryView.mText1.setBackgroundColor(

10、mColorArray<span></span>position);124 125/ galleryView.mText2.setText(mLabelArray<span></span>position);126 127/ galleryView.mText2.setBackgroundColor(mColorArray<span></span>position);128 129/ 130 131/ Log.d("111", "count="+position);132 133/

11、134 135/ return galleryView;136 137/ 138 139/ 140 141 142 143 return new GalleryViewItem(getApplicationContext(), position);144 145 146 147 );148 149 150 151 LinearLayout layout = new LinearLayout(getApplicationContext();152 153 layout.setOrientation(LinearLayout.VERTICAL);154 155 156 157 LinearLayo

12、ut.LayoutParams layoutParams = newLinearLayout.LayoutParams(158 159 LinearLayout.LayoutParams.MATCH_PARENT,160 161 LinearLayout.LayoutParams.MATCH_PARENT);162 163 164 165 layoutParams.setMargins(10, 10, 10, 10);166 167 layoutParams.weight = 1.0f;168 169 170 171 layout.addView(mGallery, layoutParams)

13、;172 173 174 175 mCheckBox = new CheckBox(getApplicationContext();176 177 mCheckBox.setText("Gallery is Circular");178 179 mCheckBox.setText("Gallery is Circular");180 181 mCheckBox.setPadding(50, 10, 0, 10);182 183 mCheckBox.setTextSize(30);184 185 mCheckBox.setChecked(true);186

14、 187 mCheckBox.setOnClickListener(new OnClickListener()188 189 190 191 Override192 193 public void onClick(View view)194 195 196 197 mGallery.setIsGalleryCircular(mCheckBox.isChecked();198 199 200 201 );202 203 204 205 layout.addView(mCheckBox, new LinearLayout.LayoutParams(206 207 LinearLayout.Layo

15、utParams.MATCH_PARENT,208 209 LinearLayout.LayoutParams.WRAP_CONTENT);210 211 212 213 setContentView(layout);214 215 216 217 218 219 private class GalleryViewItem extends TableLayout220 221 222 223 private EditText mEdit1;224 225 private TextView mText1;226 227 private TextView mText2;228 229 privat

16、e Button mButton1;230 231 private Button mButton2;232 233 234 235 public GalleryViewItem(Context context, int position)236 237 238 239 super(context);240 241 242 243 this.setOrientation(LinearLayout.VERTICAL);244 245 246 247 this.setLayoutParams(new LinearLayout.LayoutParams(248 249 LinearLayout.Lay

17、outParams.MATCH_PARENT,250 251 LinearLayout.LayoutParams.MATCH_PARENT);252 253 254 255 mEdit1 = new EditText(context);256 257 258 259 this.addView(mEdit1, new LinearLayout.LayoutParams(260 261 LinearLayout.LayoutParams.MATCH_PARENT,262 263 LinearLayout.LayoutParams.WRAP_CONTENT);264 265 266 267 mTex

18、t1 = new TextView(context);268 269 mText1.setText(mLabelArray<span></span>position);270 271 mText1.setTextSize(30);272 273 mText1.setGravity(Gravity.LEFT);274 275 mText1.setBackgroundColor(mColorArray<span></span>position);276 277 278 279 this.addView(mText1, new LinearLayout

19、.LayoutParams(280 281 LinearLayout.LayoutParams.MATCH_PARENT,282 283 LinearLayout.LayoutParams.WRAP_CONTENT);284 285 286 287 mButton1 = new Button(context);288 289 mButton1.setText("<<");290 291 mButton1.setGravity(Gravity.LEFT);292 293 mButton1.setOnClickListener(new OnClickListener

20、()294 295 296 297 Override298 299 public void onClick(View view)300 301 302 303 mGallery.movePrevious();304 305 306 307 );308 309 310 311 this.addView(mButton1, new LinearLayout.LayoutParams(312 313 LinearLayout.LayoutParams.MATCH_PARENT,314 315 LinearLayout.LayoutParams.WRAP_CONTENT);316 317 318 31

21、9 mButton2 = new Button(context);320 321 mButton2.setText(">>");322 323 mButton2.setGravity(Gravity.RIGHT);324 325 mButton2.setOnClickListener(new OnClickListener()326 327 328 329 Override330 331 public void onClick(View view)332 333 334 335 mGallery.moveNext();336 337 338 339 );340

22、341 342 343 this.addView(mButton2, new LinearLayout.LayoutParams(344 345 LinearLayout.LayoutParams.MATCH_PARENT,346 347 LinearLayout.LayoutParams.WRAP_CONTENT);348 349 350 351 mText2 = new TextView(context);352 353 mText2.setText(mLabelArray<span></span>position);354 355 mText2.setTextSi

23、ze(30);356 357 mText2.setGravity(Gravity.RIGHT);358 359 mText2.setBackgroundColor(mColorArray<span></span>position);360 361 362 363 this.addView(mText2, new LinearLayout.LayoutParams(364 365 LinearLayout.LayoutParams.MATCH_PARENT,366 367 LinearLayout.LayoutParams.MATCH_PARENT, 1);368 369

24、 370 371 372 373FlingGallery.java0001import android.content.Context;0002 0003import android.view.GestureDetector;0004 0005import android.view.KeyEvent;0006 0007import android.view.MotionEvent;0008 0009import android.view.View;0010 0011import android.view.animation.Animation;0012 0013import android.v

25、iew.animation.AnimationUtils;0014 0015import android.view.animation.Interpolator;0016 0017import android.view.animation.Transformation;0018 0019import android.widget.Adapter;0020 0021import android.widget.FrameLayout;0022 0023import android.widget.LinearLayout;0024 0025 0026 0027/ TODO:0028 0029 003

26、0 0031/ 1. In order to improve performance Cache screen bitmap and use for animation0032 0033/ 2. Establish superfluous memory allocations and delay or replace with reused objects0034 0035/ Probably need to make sure we are not allocating objects (strings, etc.) in loops0036 0037 0038 0039public cla

27、ss FlingGallery extends FrameLayout0040 00410042 0043 / Constants0044 0045 0046 0047 private final int swipe_min_distance = 120;0048 0049 private final int swipe_max_off_path = 250;0050 0051 private final int swipe_threshold_veloicty = 400;0052 0053 0054 0055 / Properties0056 0057 0058 0059 private

28、int mViewPaddingWidth = 0;0060 0061 private int mAnimationDuration = 250;0062 0063 private float mSnapBorderRatio = 0.5f;0064 0065 private boolean mIsGalleryCircular = true;0066 0067 0068 0069 / Members0070 0071 0072 0073 private int mGalleryWidth = 0;0074 0075 private boolean mIsTouched = false;007

29、6 0077 private boolean mIsDragging = false;0078 0079 private float mCurrentOffset = 0.0f;0080 0081 private long mScrollTimestamp = 0;0082 0083 private int mFlingDirection = 0;0084 0085 private int mCurrentPosition = 0;0086 0087 private int mCurrentViewNumber = 0;0088 0089 0090 0091 private Context m

30、Context;0092 0093 private Adapter mAdapter;0094 0095 private FlingGalleryView<span></span> mViews;0096 0097 private FlingGalleryAnimation mAnimation;0098 0099 private GestureDetector mGestureDetector;0100 0101 private Interpolator mDecelerateInterpolater;0102 0103 0104 0105 public FlingG

31、allery(Context context)0106 0107 0108 0109 super(context);0110 0111 0112 0113 mContext = context;0114 0115 mAdapter = null;0116 0117 0118 0119 mViews = new FlingGalleryView<span></span>3;0120 0121 mViews<span></span>0 = new FlingGalleryView(0, this);0122 0123 mViews<span&g

32、t;</span>1 = new FlingGalleryView(1, this);0124 0125 mViews<span></span>2 = new FlingGalleryView(2, this);0126 0127 0128 0129 mAnimation = new FlingGalleryAnimation();0130 0131 mGestureDetector = new GestureDetector(new FlingGestureDetector();0132 0133 mDecelerateInterpolater = Ani

33、mationUtils.loadInterpolator(mContext, android.R.anim.decelerate_interpolator);0134 0135 0136 0137 0138 0139 public void setPaddingWidth(int viewPaddingWidth)0140 0141 0142 0143 mViewPaddingWidth = viewPaddingWidth;0144 0145 0146 0147 0148 0149 public void setAnimationDuration(int animationDuration)

34、0150 0151 0152 0153 mAnimationDuration = animationDuration;0154 0155 0156 0157 0158 0159 public void setSnapBorderRatio(float snapBorderRatio)0160 0161 0162 0163 mSnapBorderRatio = snapBorderRatio;0164 0165 0166 0167 0168 0169 public void setIsGalleryCircular(boolean isGalleryCircular)0170 0171 0172

35、 0173 if (mIsGalleryCircular != isGalleryCircular)0174 0175 0176 0177 mIsGalleryCircular = isGalleryCircular;0178 0179 0180 0181 if (mCurrentPosition = getFirstPosition()0182 0183 0184 0185 / We need to reload the view immediately to the left to change it to circular view or blank0186 0187 mViews<

36、;span></span>getPrevViewNumber(mCurrentViewNumber).recycleView(getPrevPosition(mCurrentPosition); 0188 0189 0190 0191 0192 0193 if (mCurrentPosition = getLastPosition()0194 0195 0196 0197 / We need to reload the view immediately to the right to change it to circular view or blank0198 0199 m

37、Views<span></span>getNextViewNumber(mCurrentViewNumber).recycleView(getNextPosition(mCurrentPosition); 0200 0201 0202 0203 0204 0205 0206 0207 0208 0209 public int getGalleryCount()0210 0211 0212 0213 return (mAdapter = null) ? 0 : mAdapter.getCount();0214 0215 0216 0217 0218 0219 public

38、 int getFirstPosition()0220 0221 0222 0223 return 0;0224 0225 0226 0227 0228 0229 public int getLastPosition()0230 0231 0232 0233 return (getGalleryCount() = 0) ? 0 : getGalleryCount() - 1;0234 0235 0236 0237 0238 0239 private int getPrevPosition(int relativePosition)0240 0241 0242 0243 int prevPosi

39、tion = relativePosition - 1;0244 0245 0246 0247 if (prevPosition < getFirstPosition()0248 0249 0250 0251 prevPosition = getFirstPosition() - 1;0252 0253 0254 0255 if (mIsGalleryCircular = true)0256 0257 0258 0259 prevPosition = getLastPosition();0260 0261 0262 0263 0264 0265 0266 0267 return prev

40、Position;0268 0269 0270 0271 0272 0273 private int getNextPosition(int relativePosition)0274 0275 0276 0277 int nextPosition = relativePosition + 1;0278 0279 0280 0281 if (nextPosition > getLastPosition()0282 0283 0284 0285 nextPosition = getLastPosition() + 1;0286 0287 0288 0289 if (mIsGalleryCi

41、rcular = true)0290 0291 0292 0293 nextPosition = getFirstPosition();0294 0295 0296 0297 0298 0299 0300 0301 return nextPosition;0302 0303 0304 0305 0306 0307 private int getPrevViewNumber(int relativeViewNumber)0308 0309 0310 0311 return (relativeViewNumber = 0) ? 2 : relativeViewNumber - 1;0312 031

42、3 0314 0315 0316 0317 private int getNextViewNumber(int relativeViewNumber)0318 0319 0320 0321 return (relativeViewNumber = 2) ? 0 : relativeViewNumber + 1;0322 0323 0324 0325 0326 0327 Override0328 0329 protected void onLayout(boolean changed, int left, int top, int right, int bottom)0330 0331 0332

43、 0333 super.onLayout(changed, left, top, right, bottom);0334 0335 0336 0337 / Calculate our view width0338 0339 mGalleryWidth = right - left;0340 0341 0342 0343 if (changed = true)0344 0345 0346 0347 / Position views at correct starting offsets0348 0349 mViews<span></span>0.setOffset(0,

44、0, mCurrentViewNumber);0350 0351 mViews<span></span>1.setOffset(0, 0, mCurrentViewNumber);0352 0353 mViews<span></span>2.setOffset(0, 0, mCurrentViewNumber);0354 0355 0356 0357 0358 0359 0360 0361 public void setAdapter(Adapter adapter)0362 0363 0364 0365 mAdapter = adapter;0

45、366 0367 mCurrentPosition = 0;0368 0369 mCurrentViewNumber = 0;0370 0371 0372 0373 / Load the initial views from adapter0374 0375 mViews<span></span>0.recycleView(mCurrentPosition);0376 0377 mViews<span></span>1.recycleView(getNextPosition(mCurrentPosition);0378 0379 mViews<span></span>

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論