您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在Android 中實(shí)現(xiàn)3D仿Gallery效果,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
1.實(shí)現(xiàn)效果:
滾動(dòng)翻頁(yè)+ 頁(yè)面點(diǎn)擊+頁(yè)碼指示器+焦點(diǎn)控制
2.實(shí)現(xiàn)這個(gè)效果之前必須要了解
Android高級(jí)圖片滾動(dòng)控件實(shí)現(xiàn)3D版圖片輪播器這篇文章,我是基于他的代碼進(jìn)行修改的,主要為了移植到電視上做了按鍵事件和焦點(diǎn)控制。
3.具體代碼:
public class Image3DSwitchView extends LinearLayout { private int currentPage =0;//定義當(dāng)前第幾頁(yè) /** * 圖片左右兩邊的空白間距 */ public static final int IMAGE_PADDING = 5; private static final int TOUCH_STATE_REST = 0; private static final int TOUCH_STATE_SCROLLING = 1; /** * 滾動(dòng)到下一張圖片的速度 */ private static final int SNAP_VELOCITY = 600; /** * 表示滾動(dòng)到下一張圖片這個(gè)動(dòng)作 */ private static final int SCROLL_NEXT = 0; /** * 表示滾動(dòng)到上一張圖片這個(gè)動(dòng)作 */ private static final int SCROLL_PREVIOUS = 1; /** * 表示滾動(dòng)回原圖片這個(gè)動(dòng)作 */ private static final int SCROLL_BACK = 2; private static Handler handler = new Handler(); /** * 控件寬度 */ public static int mWidth; private VelocityTracker mVelocityTracker;//主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢(shì)事件)的速率 private Scroller mScroller; /** * 圖片滾動(dòng)監(jiān)聽(tīng)器,當(dāng)圖片發(fā)生滾動(dòng)時(shí)回調(diào)這個(gè)接口 */ private OnImageSwitchListener mListener; /** * 記錄當(dāng)前的觸摸狀態(tài) */ private int mTouchState = TOUCH_STATE_REST; /** * 記錄被判定為滾動(dòng)運(yùn)動(dòng)的最小滾動(dòng)值 */ private int mTouchSlop; /** * 記錄控件高度 */ private int mHeight; /** * 記錄每張圖片的寬度 */ private int mImageWidth; /** * 記錄圖片的總數(shù)量 */ private int mCount; /** * 記錄當(dāng)前顯示圖片的坐標(biāo) */ private int mCurrentImage; /** * 記錄上次觸摸的橫坐標(biāo)值 */ private float mLastMotionX; /** * 是否強(qiáng)制重新布局 */ private boolean forceToRelayout; private int[] mItems; public Image3DSwitchView(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); mScroller = new Scroller(context); //設(shè)置可以獲得焦點(diǎn) setFocusable(true); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (changed || forceToRelayout) { mCount = getChildCount(); // 圖片數(shù)量必須大于5,不然無(wú)法正常顯示 if (mCount < 5) { return; } mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); // 每張圖片的寬度設(shè)定為控件寬度的百分之六十 mImageWidth = (int) (mWidth * 0.6); if (mCurrentImage >= 0 && mCurrentImage < mCount) { mScroller.abortAnimation(); setScrollX(0); int left = -mImageWidth * 2 + (mWidth - mImageWidth) / 2; // 分別獲取每個(gè)位置上應(yīng)該顯示的圖片下標(biāo) int[] items = {getIndexForItem(1), getIndexForItem(2), getIndexForItem(3), getIndexForItem(4), getIndexForItem(5)}; mItems = items; // 通過(guò)循環(huán)為每張圖片設(shè)定位置 for (int i = 0; i < items.length; i++) { Image3DView childView = (Image3DView) getChildAt(items[i]); childView.layout(left + IMAGE_PADDING, 0, left + mImageWidth - IMAGE_PADDING, mHeight); childView.initImageViewBitmap(); left = left + mImageWidth; } refreshImageShowing(); } forceToRelayout = false; } } public interface OnFocusListener{ void onFocus(); void setValue(String value); } private OnFocusListener onFocusListener; public void setonFocusListener(OnFocusListener onFocusListener){ this.onFocusListener = onFocusListener; } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN){ switch (event.getKeyCode()){ //按下鍵 響應(yīng)回調(diào) case KeyEvent.KEYCODE_DPAD_DOWN: if (onFocusListener != null){onFocusListener.onFocus();} break; //按確定鍵 case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getContext(),"點(diǎn)擊了圖片"+(currentPage%7+1),Toast.LENGTH_SHORT).show(); break; //右鍵 case KeyEvent.KEYCODE_DPAD_RIGHT: scrollToNext(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; //左鍵 case KeyEvent.KEYCODE_DPAD_LEFT: scrollToPrevious(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; } } return true; } @Override public boolean onTouchEvent(MotionEvent event) { if (mScroller.isFinished()) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getAction(); float x = event.getX(); switch (action) { case MotionEvent.ACTION_DOWN: // 記錄按下時(shí)的橫坐標(biāo) mLastMotionX = x; break; case MotionEvent.ACTION_MOVE: int disX = (int) (mLastMotionX - x); mLastMotionX = x; scrollBy(disX, 0); // 當(dāng)發(fā)生移動(dòng)時(shí)刷新圖片的顯示狀態(tài) refreshImageShowing(); break; case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); int velocityX = (int) mVelocityTracker.getXVelocity(); if (shouldScrollToNext(velocityX)) { // 滾動(dòng)到下一張圖 scrollToNext(); } else if (shouldScrollToPrevious(velocityX)) { // 滾動(dòng)到上一張圖 scrollToPrevious(); } else { // 滾動(dòng)回當(dāng)前圖片 scrollBack(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; } } return true; } /** * 根據(jù)當(dāng)前的觸摸狀態(tài)來(lái)決定是否屏蔽子控件的交互能力。 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { return true; } float x = ev.getX(); switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mTouchState = TOUCH_STATE_REST; break; case MotionEvent.ACTION_MOVE: int xDiff = (int) Math.abs(mLastMotionX - x); if (xDiff > mTouchSlop) { mTouchState = TOUCH_STATE_SCROLLING; } break; case MotionEvent.ACTION_UP: default: mTouchState = TOUCH_STATE_REST; break; } return mTouchState != TOUCH_STATE_REST; } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); refreshImageShowing(); postInvalidate(); } } /** * 設(shè)置圖片滾動(dòng)的監(jiān)聽(tīng)器,每當(dāng)有圖片滾動(dòng)時(shí)會(huì)回調(diào)此接口。 * * @param listener 圖片滾動(dòng)監(jiān)聽(tīng)器 */ public void setOnImageSwitchListener(OnImageSwitchListener listener) { mListener = listener; } /** * 設(shè)置當(dāng)前顯示圖片的下標(biāo),注意如果該值小于零或大于等于圖片的總數(shù)量,圖片則無(wú)法正常顯示。 * * @param currentImage 圖片的下標(biāo) */ public void setCurrentImage(int currentImage) { mCurrentImage = currentImage; requestLayout(); } /** * 滾動(dòng)到下一張圖片。 */ public void scrollToNext() { if (mScroller.isFinished()) { currentPage++; int disX = mImageWidth - getScrollX(); checkImageSwitchBorder(SCROLL_NEXT); if (mListener != null) { mListener.onImageSwitch(mCurrentImage); } beginScroll(getScrollX(), 0, disX, 0, SCROLL_NEXT); } } /** * 滾動(dòng)到上一張圖片。 */ public void scrollToPrevious() { if (mScroller.isFinished()) { if (currentPage ==0){ currentPage = 7; }else currentPage--; int disX = -mImageWidth - getScrollX(); checkImageSwitchBorder(SCROLL_PREVIOUS); if (mListener != null) { mListener.onImageSwitch(mCurrentImage); } beginScroll(getScrollX(), 0, disX, 0, SCROLL_PREVIOUS); } } /** * 滾動(dòng)回原圖片。 */ public void scrollBack() { if (mScroller.isFinished()) { beginScroll(getScrollX(), 0, -getScrollX(), 0, SCROLL_BACK); } } /** * 回收所有圖片對(duì)象,釋放內(nèi)存。 */ public void clear() { for (int i = 0; i < mCount; i++) { Image3DView childView = (Image3DView) getChildAt(i); childView.recycleBitmap(); } } /** * 讓控件中的所有圖片開(kāi)始滾動(dòng)。 */ private void beginScroll(int startX, int startY, int dx, int dy, final int action) { int duration = (int) (700f / mImageWidth * Math.abs(dx)); mScroller.startScroll(startX, startY, dx, dy, duration); invalidate(); handler.postDelayed(new Runnable() { @Override public void run() { if (action == SCROLL_NEXT || action == SCROLL_PREVIOUS) { forceToRelayout = true; requestLayout(); } } }, duration); } /** * 根據(jù)當(dāng)前圖片的下標(biāo)和傳入的item參數(shù),來(lái)判斷item位置上應(yīng)該顯示哪張圖片。 * * @param item 取值范圍是1-5 * @return 對(duì)應(yīng)item位置上應(yīng)該顯示哪張圖片。 */ private int getIndexForItem(int item) { int index = -1; index = mCurrentImage + item - 3; while (index < 0) { index = index + mCount; } while (index > mCount - 1) { index = index - mCount; } return index; } /** * 刷新所有圖片的顯示狀態(tài),包括當(dāng)前的旋轉(zhuǎn)角度。 */ private void refreshImageShowing() { for (int i = 0; i < mItems.length; i++) { Image3DView childView = (Image3DView) getChildAt(mItems[i]); childView.setRotateData(i, getScrollX()); childView.invalidate(); } } /** * 檢查圖片的邊界,防止圖片的下標(biāo)超出規(guī)定范圍。 */ private void checkImageSwitchBorder(int action) { if (action == SCROLL_NEXT && ++mCurrentImage >= mCount) { mCurrentImage = 0; } else if (action == SCROLL_PREVIOUS && --mCurrentImage < 0) { mCurrentImage = mCount - 1; } } /** * 判斷是否應(yīng)該滾動(dòng)到下一張圖片。 */ private boolean shouldScrollToNext(int velocityX) { return velocityX < -SNAP_VELOCITY || getScrollX() > mImageWidth / 2; } /** * 判斷是否應(yīng)該滾動(dòng)到上一張圖片。 */ private boolean shouldScrollToPrevious(int velocityX) { return velocityX > SNAP_VELOCITY || getScrollX() < -mImageWidth / 2; } /** * 圖片滾動(dòng)的監(jiān)聽(tīng)器 */ public interface OnImageSwitchListener { /** * 當(dāng)圖片滾動(dòng)時(shí)會(huì)回調(diào)此方法 * * @param currentImage 當(dāng)前圖片的坐標(biāo) */ void onImageSwitch(int currentImage); } }
public class Image3DView extends ImageView { /** * 旋轉(zhuǎn)角度的基準(zhǔn)值 */ private static final float BASE_DEGREE = 50f; /** * 旋轉(zhuǎn)深度的基準(zhǔn)值 */ private static final float BASE_DEEP = 150f; private Camera mCamera; private Matrix mMaxtrix; private Bitmap mBitmap; /** * 當(dāng)前圖片對(duì)應(yīng)的下標(biāo) */ private int mIndex; /** * 在前圖片在X軸方向滾動(dòng)的距離 */ private int mScrollX; /** * Image3DSwitchView控件的寬度 */ private int mLayoutWidth; /** * 當(dāng)前圖片的寬度 */ private int mWidth; /** * 當(dāng)前旋轉(zhuǎn)的角度 */ private float mRotateDegree; /** * 旋轉(zhuǎn)的中心點(diǎn) */ private float mDx; /** * 旋轉(zhuǎn)的深度 */ private float mDeep; public Image3DView(Context context, AttributeSet attrs) { super(context, attrs); mCamera = new Camera(); mMaxtrix = new Matrix(); //設(shè)置可獲焦 & 可點(diǎn)擊 setFocusable(true); setClickable(true); } /** * 初始化Image3DView所需要的信息,包括圖片寬度,截取背景圖等。 */ public void initImageViewBitmap() { if (mBitmap == null) { //我們要獲取cache首先要通過(guò)setDrawingCacheEnable方法開(kāi)啟cache,然后再調(diào)用getDrawingCache方法就可以獲得view的cache圖片了。 setDrawingCacheEnabled(true); buildDrawingCache(); mBitmap = getDrawingCache(); } mLayoutWidth = Image3DSwitchView.mWidth; mWidth = getWidth() + Image3DSwitchView.IMAGE_PADDING * 2; } /** * 設(shè)置旋轉(zhuǎn)角度。 * * @param index 當(dāng)前圖片的下標(biāo) * @param scrollX 當(dāng)前圖片在X軸方向滾動(dòng)的距離 */ public void setRotateData(int index, int scrollX) { mIndex = index; mScrollX = scrollX; } /** * 回收當(dāng)前的Bitmap對(duì)象,以釋放內(nèi)存。 */ public void recycleBitmap() { if (mBitmap != null && !mBitmap.isRecycled()) { mBitmap.recycle(); } } @Override public void setImageResource(int resId) { super.setImageResource(resId); mBitmap = null; initImageViewBitmap(); } @Override public void setImageBitmap(Bitmap bm) { super.setImageBitmap(bm); mBitmap = null; initImageViewBitmap(); } @Override public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); mBitmap = null; initImageViewBitmap(); } @Override public void setImageURI(Uri uri) { super.setImageURI(uri); mBitmap = null; initImageViewBitmap(); } @Override protected void onDraw(Canvas canvas) { if (mBitmap == null) { // 如果Bitmap對(duì)象還不存在,先使用父類的onDraw方法進(jìn)行繪制 super.onDraw(canvas); } else { if (isImageVisible()) { // 繪圖時(shí)需要注意,只有當(dāng)圖片可見(jiàn)的時(shí)候才進(jìn)行繪制,這樣可以節(jié)省運(yùn)算效率 computeRotateData(); mCamera.save(); mCamera.translate(0.0f, 0.0f, mDeep); mCamera.rotateY(mRotateDegree); mCamera.getMatrix(mMaxtrix); mCamera.restore(); mMaxtrix.preTranslate(-mDx, -getHeight() / 2); mMaxtrix.postTranslate(mDx, getHeight() / 2); canvas.drawBitmap(mBitmap, mMaxtrix, null); } } } /** * 在這里計(jì)算所有旋轉(zhuǎn)所需要的數(shù)據(jù)。 */ private void computeRotateData() { float degreePerPix = BASE_DEGREE / mWidth; float deepPerPix = BASE_DEEP / ((mLayoutWidth - mWidth) / 2); switch (mIndex) { case 0: mDx = mWidth; mRotateDegree = 360f - (2 * mWidth + mScrollX) * degreePerPix; if (mScrollX < -mWidth) { mDeep = 0; } else { mDeep = (mWidth + mScrollX) * deepPerPix; } break; case 1: if (mScrollX > 0) { mDx = mWidth; mRotateDegree = (360f - BASE_DEGREE) - mScrollX * degreePerPix; mDeep = mScrollX * deepPerPix; } else { if (mScrollX < -mWidth) { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = (-mScrollX - mWidth) * degreePerPix; } else { mDx = mWidth; mRotateDegree = 360f - (mWidth + mScrollX) * degreePerPix; } mDeep = 0; } break; case 2: if (mScrollX > 0) { mDx = mWidth; mRotateDegree = 360f - mScrollX * degreePerPix; mDeep = 0; if (mScrollX > mWidth) { mDeep = (mScrollX - mWidth) * deepPerPix; } } else { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = -mScrollX * degreePerPix; mDeep = 0; if (mScrollX < -mWidth) { mDeep = -(mWidth + mScrollX) * deepPerPix; } } break; case 3: if (mScrollX < 0) { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = BASE_DEGREE - mScrollX * degreePerPix; mDeep = -mScrollX * deepPerPix; } else { if (mScrollX > mWidth) { mDx = mWidth; mRotateDegree = 360f - (mScrollX - mWidth) * degreePerPix; } else { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = BASE_DEGREE - mScrollX * degreePerPix; } mDeep = 0; } break; case 4: mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = (2 * mWidth - mScrollX) * degreePerPix; if (mScrollX > mWidth) { mDeep = 0; } else { mDeep = (mWidth - mScrollX) * deepPerPix; } break; } } /** * 判斷當(dāng)前圖片是否可見(jiàn)。 * * @return 當(dāng)前圖片可見(jiàn)返回true,不可見(jiàn)返回false。 */ private boolean isImageVisible() { boolean isVisible = false; switch (mIndex) { case 0: if (mScrollX < (mLayoutWidth - mWidth) / 2 - mWidth) { isVisible = true; } else { isVisible = false; } break; case 1: if (mScrollX > (mLayoutWidth - mWidth) / 2) { isVisible = false; } else { isVisible = true; } break; case 2: if (mScrollX > mLayoutWidth / 2 + mWidth / 2 || mScrollX < -mLayoutWidth / 2 - mWidth / 2) { isVisible = false; } else { isVisible = true; } break; case 3: if (mScrollX < -(mLayoutWidth - mWidth) / 2) { isVisible = false; } else { isVisible = true; } break; case 4: if (mScrollX > mWidth - (mLayoutWidth - mWidth) / 2) { isVisible = true; } else { isVisible = false; } break; } return isVisible; } }
public class MainActivity extends AppCompatActivity { private Button btn; private Image3DSwitchView image3DSwitchView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); image3DSwitchView = (Image3DSwitchView) findViewById(R.id.image_switch_view); btn.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction()== KeyEvent.ACTION_DOWN) image3DSwitchView.requestFocus(); return true; } }); //設(shè)置監(jiān)聽(tīng),并實(shí)現(xiàn)接口 image3DSwitchView.setonFocusListener(new Image3DSwitchView.OnFocusListener() { @Override public void onFocus() { btn.requestFocus(); } @Override public void setValue(String value) { btn.setText(value+"/7"); } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <com.example.rotate3d.Image3DSwitchView android:layout_marginTop="100dp" android:id="@+id/image_switch_view" android:layout_width="wrap_content" android:layout_height="470dp" > <com.example.rotate3d.Image3DView android:id="@+id/image1" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image1" /> <com.example.rotate3d.Image3DView android:id="@+id/image2" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image2" /> <com.example.rotate3d.Image3DView android:id="@+id/image3" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image3" /> <com.example.rotate3d.Image3DView android:id="@+id/image4" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image4" /> <com.example.rotate3d.Image3DView android:id="@+id/image5" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image5" /> <com.example.rotate3d.Image3DView android:id="@+id/image6" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image6" /> <com.example.rotate3d.Image3DView android:id="@+id/image7" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/image7" /> </com.example.rotate3d.Image3DSwitchView> <Button android:id="@+id/btn" android:text="1/7" android:textSize="32dp" android:textColor="#ffffff" android:layout_below="@+id/image_switch_view" android:background="@drawable/button_style" android:layout_width="match_parent" android:layout_height="100dp" /> </RelativeLayout>
4.自己的成長(zhǎng)點(diǎn):
①響應(yīng)遙控器按鍵事件:重寫(xiě)Image3DSwitchView類的dispatchKeyEvent()函數(shù)
@Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN){ switch (event.getKeyCode()){ //按下鍵 響應(yīng)回調(diào) case KeyEvent.KEYCODE_DPAD_DOWN: if (onFocusListener != null){onFocusListener.onFocus();} break; //按確定鍵 case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getContext(),"點(diǎn)擊了圖片"+(currentPage%7+1),Toast.LENGTH_SHORT).show(); break; //右鍵 case KeyEvent.KEYCODE_DPAD_RIGHT: scrollToNext(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; //左鍵 case KeyEvent.KEYCODE_DPAD_LEFT: scrollToPrevious(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; } } return true; }
注意:要能響應(yīng)點(diǎn)擊事件的前提必須是控件可以獲得焦點(diǎn),所以在構(gòu)造函數(shù)中加上
setFocusable(true);
②回調(diào)機(jī)制:類似Android TV開(kāi)發(fā):使用RecycleView實(shí)現(xiàn)橫向的Listview并響應(yīng)點(diǎn)擊事件的代碼的原理。 越來(lái)越發(fā)現(xiàn)回調(diào)技術(shù)的好用:輕松通過(guò)接口函數(shù)里面的參數(shù)把動(dòng)態(tài)變化的變量傳遞到mainActivity
public interface OnFocusListener{ void onFocus();//處理焦點(diǎn) void setValue(String value);//處理當(dāng)前頁(yè)碼 } private OnFocusListener onFocusListener; public void setonFocusListener(OnFocusListener onFocusListener){ this.onFocusListener = onFocusListener; www.jb51.net/article/140153.htm} @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN){ switch (event.getKeyCode()){ //按下鍵 響應(yīng)回調(diào) case KeyEvent.KEYCODE_DPAD_DOWN: if (onFocusListener != null){onFocusListener.onFocus();} break; //按確定鍵 case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getContext(),"點(diǎn)擊了圖片"+(currentPage%7+1),Toast.LENGTH_SHORT).show(); break; //右鍵 case KeyEvent.KEYCODE_DPAD_RIGHT: scrollToNext(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; //左鍵 case KeyEvent.KEYCODE_DPAD_LEFT: scrollToPrevious(); if (onFocusListener !=null){ onFocusListener.setValue((currentPage%7+1)+""); } break; } } return true; }
在acitivity中調(diào)用
image3DSwitchView.setonFocusListener(new Image3DSwitchView.OnFocusListener() { @Override public void onFocus() { btn.requestFocus();//讓Image3DSwitchView控件下的按鈕獲得焦點(diǎn) } @Override public void setValue(String value) { btn.setText(value+"/7");//給按鈕設(shè)置 第幾頁(yè)/共幾頁(yè) } });
以上就是怎么在Android 中實(shí)現(xiàn)3D仿Gallery效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。