gpt4 book ai didi

android - 具有旋转变换的 ViewPager

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:43 25 4
gpt4 key购买 nike

我想创建 ViewPager,它在滑动时围绕它的 getWidth()getHeight() 轴旋转。我试过了,但所有这些转换只能通过垂直和水平的各种效果实现。

有什么方法或提示可以开发这种ViewPager吗?

EDIT- 解释它会是什么样的轮换

Axis - getWidth, getHeight 应该是枢轴点,一旦我滑动它应该旋转,对于下一页它应该从 (0, getHeight) 轴旋转。为了简化,考虑这个简单的场景 -

  1. 有一个圆圈,里面有4个部分
  2. 目前只有1部分正在显示,其他3部分在屏幕外
  3. 一旦我从右向左滑动,ViewPager 应该从第 1 页旋转到第 2 页。
  4. 如果我从左向右滑动,ViewPager 应该从第 1 页旋转到第 4 页。
  5. 每次退出和进入页面都应从该圆圈的中心旋转。
  6. 圆心位于第一页的getWidthgetHeight。因此,对于下一页,它将始终是 0getHeight 作为轴,而对于上一页,它将始终是 getWidth0作为轴。

最佳答案

更新据我所知,你想要这样的过渡。如下所示,实现了 ViewPager.PageTransformer

enter image description here

这是 ViewPager.PageTransformer 类:

public class RotationCircleTransformer implements ViewPager.PageTransformer {


public RotationCircleTransformer() {
}

public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
view.setPivotX((float) pageWidth);
view.setPivotY((float) pageHeight);

if (position < -1) { //[-infinity,1)
//off to the left by a lot
view.setRotation(0);
view.setAlpha(0);
} else if (position <= 1) { //[-1,1]
view.setTranslationX((-position) * pageWidth); //shift the view over
view.setRotation(position * (90)); //rotate it
// Fade the page relative to its distance from the center
view.setAlpha(Math.max(1, 1 - Math.abs(position) / 3));
} else { //(1, +infinity]
//off to the right by a lot
view.setRotation(0);
view.setAlpha(0);
}
}
}

background_quarter.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="300dp" android:width="300dp"/>
<solid android:color="#00F"/>
<corners android:topLeftRadius="300dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp" android:bottomLeftRadius="0dp"/>
</shape>

activity_circle_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="300dp" />


</FrameLayout>

fragment_content.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/bg"
android:background="@drawable/background_quarter">

<TextView
android:id="@+id/text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:textColor="@android:color/white"
android:text="" />

</FrameLayout>

CircleFragment.class

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


public class CircleFragment extends Fragment {

private String typeString;

public CircleFragment() {
}

public static CircleFragment newInstance(String type) {
CircleFragment fragment = new CircleFragment();
Bundle args = new Bundle();
args.putString("type", type);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
typeString = getArguments().getString("type","1");
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
;
TextView text = (TextView) view.findViewById(R.id.text);
FrameLayout bg = (FrameLayout) view.findViewById(R.id.bg);
switch (typeString) {
case "1":
bg.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.background_quarter));
break;
case "2":
bg.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.background_quarter));
break;
case "3":
bg.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.background_quarter));
break;
case "4":
bg.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.background_quarter));
break;
}
text.setText(typeString);
return view;
}

}

CirclePagerActivity.class

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class CirclePagerActivity extends AppCompatActivity {

ViewPager mPager;
CirclePagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circle_pager);
mPager = (ViewPager) findViewById(R.id.pager);
List<Fragment> fragments = new ArrayList<>();
fragments.add(ContentFragment.newInstance("1"));
fragments.add(ContentFragment.newInstance("2"));
fragments.add(ContentFragment.newInstance("3"));
fragments.add(ContentFragment.newInstance("4"));
mAdapter = new CirclePagerAdapter(getSupportFragmentManager(), fragments);
mPager.setAdapter(mAdapter);

mPager.setPageTransformer(true, new RotationCircleTransformer());
}

@Override
protected void onDestroy() {
super.onDestroy();
}

static class CirclePagerAdapter extends FragmentStatePagerAdapter {

List<Fragment> mFrags = new ArrayList<>();

public CirclePagerAdapter(FragmentManager fm, List<Fragment> frags) {
super(fm);
mFrags = frags;
}

@Override
public Fragment getItem(int position) {
int index = position % mFrags.size();
return mFrags.get(index);
}

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

}
}

如果没有找到合适的ViewPager变换动画,可以自己实现ViewPager.PageTransformer

There is大量信息和多个示例。

还有 here您可以找到有用的教程。


你能更详细地解释一下这部分吗?我会根据您的解释更新我的答案(视觉解释更好)。

rotates around it's getWidth() and getHeight() axis.

关于android - 具有旋转变换的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com