溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android中怎么利用Gallery實(shí)現(xiàn)幻燈片效果

發(fā)布時(shí)間:2021-06-26 15:50:00 來(lái)源:億速云 閱讀:317 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

今天就跟大家聊聊有關(guān)Android中怎么利用Gallery實(shí)現(xiàn)幻燈片效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

(1)幻燈片效果的實(shí)現(xiàn):

自定義Gallery:DetailGallery.java

可視界面:ImgSwitchActivity.java

適配類(lèi):GalleryIndexAdapter.java

1)自定義Gallery主要重寫(xiě)onFling通過(guò)按下和松手的位置不同比較是向右移動(dòng)還是向左移動(dòng),部分代碼如下:

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {         return e2.getX() > e1.getX();     }     @Override     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,             float velocityY) {         int kEvent;         if (isScrollingLeft(e1, e2)) {             kEvent = KeyEvent.KEYCODE_DPAD_LEFT;         } else {             kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;         }         onKeyDown(kEvent, null);         return true;     }

2)在適配類(lèi)  GalleryIndexAdapter主要完成幻燈片的循環(huán)播放,在getCount里面返回值返回Integer.MAX_VALUE,然后在getView里面根據(jù)position與傳進(jìn)來(lái)初始圖片個(gè)數(shù)進(jìn)行余數(shù)計(jì)算得到每次循環(huán)到哪張圖片。部分代碼如下:

@Override         public int getCount() {             // TODO Auto-generated method stub             return Integer.MAX_VALUE;         }         ……         @Override         public View getView(int position, View convertView, ViewGroup arg2) {             // TODO Auto-generated method stub             ImageView imageView = new ImageView(context); imageView.setBackgroundResource(imagList.get(position%imagList.size()));             imageView.setScaleType(ScaleType.FIT_XY);             imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT                     , Gallery.LayoutParams.WRAP_CONTENT));             return imageView;         }

3)在可視界面里面實(shí)現(xiàn)邏輯控制,通過(guò)定時(shí)器定時(shí)刷新幻燈片,定時(shí)器通過(guò)定時(shí)發(fā)送消息,消息接受處理機(jī)制接收到消息之后,就模擬滑動(dòng)事件,調(diào) 用Gallery的onFling方法實(shí)現(xiàn)圖片自動(dòng)切換效果。選擇按鈕的顯示效果(RadioButton)需要在Gallery的 setOnItemSelectedListener進(jìn)行處理。

//定時(shí)器和事件處理5秒刷新一次幻燈片     /** 展示圖控制器,實(shí)現(xiàn)展示圖切換 */         final Handler handler_gallery = new Handler() {             public void handleMessage(Message msg) {                 /* 自定義屏幕按下的動(dòng)作 */                 MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),                         SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,                         89.333336f, 265.33334f, 0);                 /* 自定義屏幕放開(kāi)的動(dòng)作 */                 MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),                         SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,                         300.0f, 238.00003f, 0);                 myGallery.onFling(e2, e1, -800, 0);                 /* 給gallery添加按下和放開(kāi)的動(dòng)作,實(shí)現(xiàn)自動(dòng)滑動(dòng) */                 super.handleMessage(msg);             }         };         protected void onResume() {             autogallery();             super.onResume();         };         private void autogallery() {             /* 設(shè)置定時(shí)器,每5秒自動(dòng)切換展示圖 */             Timer time = new Timer();             TimerTask task = new TimerTask() {                 @Override                 public void run() {                     Message m = new Message();                     handler_gallery.sendMessage(m);                 }             };             time.schedule(task, 8000, 5000);         }     //指示按鈕和gallery初始化過(guò)程以及事件監(jiān)聽(tīng)添加過(guò)程     //初始化         void init(){             myGallery = (DetailGallery)findViewById(R.id.myGallery);             gallery_points = (RadioGroup) this.findViewById(R.id.galleryRaidoGroup);             ArrayList<Integer> list = new ArrayList<Integer>();             list.add(R.drawable.banner1);             list.add(R.drawable.banner2);             list.add(R.drawable.banner3);             list.add(R.drawable.banner4);             GalleryIndexAdapter adapter = new GalleryIndexAdapter(list, context);             myGallery.setAdapter(adapter);             //設(shè)置小按鈕             gallery_point = new RadioButton[list.size()];             for (int i = 0; i < gallery_point.length; i++) {                 layout = (LinearLayout) inflater.inflate(R.layout.gallery_icon, null);                 gallery_point[i] = (RadioButton) layout.findViewById(R.id.gallery_radiobutton);                 gallery_point[i].setId(i);/* 設(shè)置指示圖按鈕ID */                 int wh = Tool.dp2px(context, 10);                 RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(wh, wh); // 設(shè)置指示圖大小                 gallery_point[i].setLayoutParams(layoutParams);                 layoutParams.setMargins(4, 0, 4, 0);// 設(shè)置指示圖margin值                 gallery_point[i].setClickable(false);/* 設(shè)置指示圖按鈕不能點(diǎn)擊 */                 layout.removeView(gallery_point[i]);//一個(gè)子視圖不能指定了多個(gè)父視圖                 gallery_points.addView(gallery_point[i]);/* 把已經(jīng)初始化的指示圖動(dòng)態(tài)添加到指示圖的RadioGroup中 */             }         }         //添加事件         void addEvn(){             myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {                 @Override                 public void onItemSelected(AdapterView<?> arg0, View arg1,                         int arg2, long arg3) {                     // TODO Auto-generated method stub gallery_points.check(gallery_point[arg2%gallery_point.length].getId());                 }                 @Override                 public void onNothingSelected(AdapterView<?> arg0) {                     // TODO Auto-generated method stub                 }             });         }

(2)商品圖片滑動(dòng)實(shí)現(xiàn)過(guò)程:

圖片滑動(dòng)效果和上面的幻燈片效果非常的類(lèi)似,只是在邏輯處理和界面上有一些小小的區(qū)別。

1)適配器類(lèi)GalleryAdapter.java上面進(jìn)行了圖片縮放處理,節(jié)省了內(nèi)存開(kāi)銷(xiāo),又可把圖片按照自己的要求縮放。

//由于是測(cè)試case,所以圖片都是寫(xiě)死的為了區(qū)別,在position = 1的時(shí)候換了一張圖片     public View getView(int position, View convertView, ViewGroup parent) {                 // TODO Auto-generated method stub                 ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.img,                         null);                 Bitmap bitmap = null;                 try {                     if(position == 1 ){                         bitmap = BitmapFactory.decodeStream(assetManager.open("xpic11247_s.jpg"));                         imageView.setTag("xpic11247_s.jpg");                     }                     else{                         bitmap = BitmapFactory.decodeStream(assetManager.open("item0_pic.jpg"));                         imageView.setTag("item0_pic.jpg");                     }                 } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                 }                 // 加載圖片之前進(jìn)行縮放                 int width = bitmap.getWidth();                 int height = bitmap.getHeight();                 float newHeight = 200;                 float newWidth = width*newHeight/height;                 float scaleWidth = ((float) newWidth) / width;                 float scaleHeight = ((float) newHeight) / height;                 // 取得想要縮放的matrix參數(shù)                 Matrix matrix = new Matrix();                 matrix.postScale(scaleWidth, scaleHeight);                 // 得到新的圖片                 Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);                 System.out.println(newbm.getHeight()+"-----------"+newbm.getWidth());                 imageView.setImageBitmap(newbm);                 // }                 return imageView;             }

2)添加了一個(gè)相框效果,如果圖片加載失敗,就會(huì)出現(xiàn)一個(gè)圖片壓縮之后大小相等的相框圖片。

<?xml version="1.0" encoding="utf-8"?>     <ImageView xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/waterfall_image"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="@drawable/image_border"         >     </ImageView>

三、開(kāi)發(fā)中遇到一些問(wèn)題

(1)layout.removeView(gallery_point[i]);//一個(gè)子視圖不能指定了多個(gè)父視圖

如果需要把當(dāng)前子childview添加到另外一個(gè)view里面去,則必須在當(dāng)前的父View里面移除掉當(dāng)前的childView,如果不進(jìn)行這樣處理則 會(huì)拋出Caused by: java.lang.IllegalStateException異常,提示The specified child  already has a parent. You must call removeView() on the child's parent  first.

(2)在進(jìn)行圖片縮放的時(shí)候,記得處理好dp和px直接的轉(zhuǎn)換。

看完上述內(nèi)容,你們對(duì)Android中怎么利用Gallery實(shí)現(xiàn)幻燈片效果有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI