问题描述
- 设计一个android引导页面的动画,最好是透明渐变动画效果,求大神帮忙!
-
设计一个android引导页面的动画,最好是透明渐变动画效果,大概样式就像APP微信精选那个引导页面那种样式,求大神帮忙给个Demo!
解决方案
之前回复的都回复不了,现在重新再给你回复一遍吧,首先创建三个Fragment,我现在给你举一个例子:
public class WelcomeFragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome1, null);
return view;
}
}
下面是fragment对应的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/jpg_1" />
</LinearLayout>
同理,你自己新建另外两个Fragment和布局文件,下面是一个引导页GuideActivity:
package com.example.splashdemo;
import java.util.ArrayList;
import java.util.List;
import com.nineoldandroids.view.ViewHelper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
public class GuideActivity extends FragmentActivity {
private WelcomeFragment1 fragment1;
private WelcomeFragment2 fragment2;
private WelcomeFragment3 fragment3;
private FragAdapter adapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
viewPager = (ViewPager) findViewById(R.id.viewpager);
List<Fragment> fragments = new ArrayList<Fragment>();
fragment1 = new WelcomeFragment1();
fragment2 = new WelcomeFragment2();
fragment3 = new WelcomeFragment3();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
if (adapter == null) {
adapter = new FragAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
viewPager.setPageTransformer(true, new RotateDownPageTransformer());
}else{
adapter.notifyDataSetChanged();
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
protected void onResume() {
super.onResume();
}
public class FragAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments;
public FragAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
return mFragments.get(arg0);
}
@Override
public int getCount() {
return mFragments.size();
}
}
public class RotateDownPageTransformer implements ViewPager.PageTransformer
{
private static final float ROT_MAX = 20.0f;
private float mRot;
public void transformPage(View view, float position)
{
Log.e("TAG", view + " , " + position + "");
if (position < -1)
{ // [-Infinity,-1)
// This page is way off-screen to the left.
ViewHelper.setRotation(view, 0);
} else if (position <= 1) // a页滑动至b页 ; a页从 0.0 ~ -1 ;b页从1 ~ 0.0
{ // [-1,1]
// Modify the default slide transition to shrink the page as well
if (position < 0)
{
mRot = (ROT_MAX * position);
ViewHelper.setPivotX(view, view.getMeasuredWidth() * 0.5f);
ViewHelper.setPivotY(view, view.getMeasuredHeight());
ViewHelper.setRotation(view, mRot);
} else
{
mRot = (ROT_MAX * position);
ViewHelper.setPivotX(view, view.getMeasuredWidth() * 0.5f);
ViewHelper.setPivotY(view, view.getMeasuredHeight());
ViewHelper.setRotation(view, mRot);
}
// Scale the page down (between MIN_SCALE and 1)
// Fade the page relative to its size.
} else
{ // (1,+Infinity]
// This page is way off-screen to the right.
ViewHelper.setRotation(view, 0);
}
}
}
}
不过你这个时候还需要到网上下载一个jar包,nineoldAndroid.jar,这个jar包主要是兼容3.0以下的,还有上面使用的ViewHelper是依赖于这个包的,你可以用我给的代码试试,不懂的再问。
解决方案二:
用ViewPager,设置监听器OnPageChangeListener,里面有个方法可以知道页面滑动切换的程度,此时设置透明度,形成动画
建议看看相关资料
解决方案三:
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE)
* (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
通过mViewPager.setPageTransformer(true, new DepthPageTransformer());调用就可以实现比较酷炫的动画效果。
解决方案四:
如果有不懂的再问我。
解决方案五:
我被禁言了是怎么回事,回复不了了。
时间: 2025-01-08 15:49:37