欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在Android中使用ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果

怎么在Android中使用ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)巴林右旗,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

第一步、創(chuàng)建卡片viewpager適配器的itemview的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bitmap"
  android:gravity="center">

  <ImageView
    android:id="@+id/home_viewpage_item_img"
    android:paddingBottom="@dimen/dp_82"
    android:paddingTop="@dimen/dp_82"
    android:paddingLeft="@dimen/dp_44"
    android:paddingRight="@dimen/dp_44"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>

第二步、創(chuàng)建適配器:

class CardAdapter(var context: Context) : PagerAdapter() {
  val horoscopestrImgs = intArrayOf(R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
      R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher)

  override fun isViewFromObject(view: View, `object`: Any): Boolean {
    return view === `object`
  }

  override fun getCount(): Int {
    return 12 * 30
  }

  override fun instantiateItem(container: ViewGroup, position: Int): Any {
    val view = LayoutInflater.from(context).inflate(R.layout.home_viewpage_item, null)


    view.home_viewpage_item_img.setImageResource(horoscopestrImgs.get(position%12))

    container.addView(view)
    return view
  }

  override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    container.removeView(`object` as View)

  }
}

第三步、創(chuàng)建放viewpager控件的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:background="#fff"
  android:layout_height="match_parent" >


  <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:overScrollMode="never"
    android:paddingBottom="@dimen/dp_240"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="@dimen/dp_60" />

</RelativeLayout>

第四步、創(chuàng)建viewpager滑動(dòng)切換動(dòng)畫效果:

class CardTransformer(var context: Context) :ViewPager.PageTransformer{
  val TAG = "CardTransformer"
  private val MAX_SCALE = 1.0f
  private val MIN_SCALE = 0.85f//0.85f
  var animator:ObjectAnimator?=null


  override fun transformPage(page: View, position: Float) {
    //設(shè)置了內(nèi)間距  有0.15的偏差
     var pos=position -0.15.toFloat()

    if ( pos <= 1) {
      val scaleFactor = MIN_SCALE + (1 - Math.abs(pos)) * (MAX_SCALE - MIN_SCALE)

      page.scaleX = scaleFactor //縮放效果
      if (pos > 0) {
        page.translationX = -scaleFactor * 2
      } else if (pos < 0 && pos > -1) {
        page.translationX = scaleFactor * 2
      }
      page.scaleY = scaleFactor


    } else {
      page.scaleX = MIN_SCALE
      page.scaleY = MIN_SCALE
    }

  }
}

第五步、開始調(diào)用實(shí)現(xiàn)卡片效果的關(guān)鍵代碼:

class MainActivity : Activity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main7)


    //設(shè)置ViewPager中兩頁之間的距離
    // viewpager?.setPageMargin(80)

    //自定義ViewPager的頁面切換動(dòng)畫
    viewpager?.setPageTransformer(false, CardTransformer(applicationContext))
    //設(shè)置viewpager預(yù)加載的頁數(shù)
    viewpager?.offscreenPageLimit = 5
    viewpager?.currentItem = 12 * 15
    viewpager?.setAdapter(CardAdapter(this))
  }


}

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

關(guān)于怎么在Android中使用ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前標(biāo)題:怎么在Android中使用ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果
分享鏈接:http://www.chinadenli.net/article36/jcoipg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器虛擬主機(jī)域名注冊(cè)做網(wǎng)站企業(yè)建站App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司