直播軟體搭建,橫版自動滑動的輪播圖

zhibo系統開發發表於2022-03-03

直播軟體搭建,橫版自動滑動的輪播圖實現的相關程式碼

佈局檔案很簡單,就一個viewpager,不過需要設定android:clipChildren為false,以便能顯示出兩邊的view,兩邊各留了60dp。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#ffffff"
    android:clipChildren="false"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:clipChildren="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

再來看下具體的程式碼實現:

public class MainActivity extends Activity {
    private ViewPager viewPager;
    private int[] imgs = new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgs.length;
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(imgs[position]);
                container.addView(imageView);
                return imageView;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        viewPager.setPageTransformer(true,new ViewPagerTrans());
    }
    class ViewPagerTrans implements ViewPager.PageTransformer{
        private float MIN_ALPHA = 0.5f;//漸變
        private float MIN_SCALE = 0.8f;//縮放
        @Override
        public void transformPage(View page, float position) {
//            float alpha = (1 - Math.abs(position)) * MIN_ALPHA + MIN_ALPHA;
//            float leftScale =  MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
            if(position < -1){//左側劃出螢幕部分
                page.setAlpha(MIN_ALPHA);
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
            } else if(position <=1){
                if(position <= 0){//滑動過程中左半部分  [-1~0]
                    float leftAlpha = (1 + position) * MIN_ALPHA + MIN_ALPHA;
                    page.setAlpha(leftAlpha);
                    float leftScale =  MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                    page.setScaleX(leftScale);
                    page.setScaleY(leftScale);
                } else {//滑動過程中又半部分 [0~1]
                    float rightAlpha = (1 - position) * MIN_ALPHA + MIN_ALPHA;
                    page.setAlpha(rightAlpha);
                    float rightScale = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                    page.setScaleX(rightScale);
                    page.setScaleY(rightScale);
                }
            } else {//右側超出螢幕部分
                page.setAlpha(MIN_ALPHA);
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
            }
        }
    }
}


以上就是直播軟體搭建,橫版自動滑動的輪播圖實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2862853/,如需轉載,請註明出處,否則將追究法律責任。

相關文章