-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hongyangAndroid
committed
May 7, 2016
1 parent
f866684
commit f338d87
Showing
53 changed files
with
1,152 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,104 @@ | ||
# MagicViewPager | ||
单页显示3个Item的ViewPager炫酷切换效果,适用于Banner等。 | ||
|
||
## 效果图 | ||
|
||
* Rotate Y | ||
|
||
<img src="screenshot/rotate_y.gif" width="420px"/> | ||
|
||
|
||
* Rotate Down | ||
|
||
<img src="screenshot/rotate_down.gif" width="420px"/> | ||
|
||
* Rotate Up | ||
|
||
<img src="screenshot/rotate_up.gif" width="420px"/> | ||
|
||
* Alpha | ||
|
||
<img src="screenshot/alpha.gif" width="420px"/> | ||
|
||
* ScaleIn | ||
|
||
<img src="screenshot/scaleIn.gif" width="420px"/> | ||
|
||
|
||
* ScaleIn + Alpha + Rotate Down | ||
|
||
<img src="screenshot/zuhe.gif" width="420px"/> | ||
|
||
## 使用 | ||
|
||
###(1)引入 | ||
|
||
``` | ||
compile `com.zhy:magic-viewpager:1.0.0` | ||
``` | ||
|
||
###(2)示例 | ||
|
||
* 布局文件 | ||
|
||
``` | ||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="160dp" | ||
android:clipChildren="false" | ||
android:layout_centerInParent="true" | ||
android:background="#aadc71ff" | ||
> | ||
<android.support.v4.view.ViewPager | ||
android:id="@+id/id_viewpager" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="60dp" | ||
android:layout_marginRight="60dp" | ||
android:layout_height="120dp" | ||
android:layout_gravity="center" | ||
> | ||
</android.support.v4.view.ViewPager> | ||
</FrameLayout> | ||
``` | ||
注意外层`android:layout_centerInParent="true"`. | ||
|
||
* 编码 | ||
|
||
``` | ||
mViewPager.setPageMargin(20);//设置page间间距,自行根据需求设置 | ||
mViewPager.setOffscreenPageLimit(3);//>=3 | ||
mViewPager.setAdapter...//写法不变 | ||
//setPageTransformer 决定动画效果 | ||
mViewPager.setPageTransformer(true, new | ||
RotateDownPageTransformer()); | ||
``` | ||
|
||
|
||
|
||
##目前可选动画 | ||
|
||
* AlphaPageTransformer | ||
* RotateDownPageTransformer | ||
* RotateUpPageTransformer | ||
* RotateYTransformer | ||
* NonPageTransformer | ||
* ScaleInTransformer | ||
|
||
其中AlphaPageTransformer、NonPageTransformer、单独使用,需要调用` mViewPager.setClipChildren(false);`,其余不用。 | ||
|
||
动画间可以自由组合,例如: | ||
|
||
``` | ||
mViewPager.setPageTransformer(true, | ||
new RotateDownPageTransformer(new AlphaPageTransformer(new ScaleInTransformer()))); | ||
``` | ||
|
||
##致谢 | ||
|
||
本文是我在审公众号投稿时,由本篇文章产生灵感:[Android超高仿QQ附近的人搜索展示(一)](http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820073&idx=1&sn=9e084723624180f7ab28e54f2aef132c&scene=23&srcid=0506b08maFirw2pBvnewcDsp#rd) ,ps:欢迎关注我的公众号。 | ||
|
||
除上文外编码过程中参考: | ||
|
||
* http://blog.csdn.net/lmj623565791/article/details/40411921/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "23.0.1" | ||
|
||
defaultConfig { | ||
applicationId "com.zhy.magicviewpager.sample" | ||
minSdkVersion 10 | ||
targetSdkVersion 23 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', include: ['*.jar']) | ||
compile 'com.android.support:appcompat-v7:23.1.0' | ||
compile project(':library') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
15 changes: 15 additions & 0 deletions
15
app/src/androidTest/java/com/zhy/magicviewpager/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.zhy.magicviewpager; | ||
|
||
import android.app.Application; | ||
import android.test.ApplicationTestCase; | ||
|
||
/** | ||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> | ||
*/ | ||
public class ApplicationTest extends ApplicationTestCase<Application> | ||
{ | ||
public ApplicationTest() | ||
{ | ||
super(Application.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.zhy.magicviewpager.sample" > | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme" > | ||
<activity | ||
android:name=".MainActivity" | ||
android:label="@string/app_name" > | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
127 changes: 127 additions & 0 deletions
127
app/src/main/java/com/zhy/magicviewpager/sample/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.zhy.magicviewpager.sample; | ||
|
||
import android.os.Bundle; | ||
import android.support.v4.view.PagerAdapter; | ||
import android.support.v4.view.ViewPager; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
|
||
import com.zhy.magicviewpager.transformer.AlphaPageTransformer; | ||
import com.zhy.magicviewpager.transformer.NonPageTransformer; | ||
import com.zhy.magicviewpager.transformer.RotateDownPageTransformer; | ||
import com.zhy.magicviewpager.transformer.RotateUpPageTransformer; | ||
import com.zhy.magicviewpager.transformer.RotateYTransformer; | ||
import com.zhy.magicviewpager.transformer.ScaleInTransformer; | ||
|
||
public class MainActivity extends AppCompatActivity | ||
{ | ||
|
||
private ViewPager mViewPager; | ||
private PagerAdapter mAdapter; | ||
|
||
int[] imgRes = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, | ||
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) | ||
{ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
mViewPager = (ViewPager) findViewById(R.id.id_viewpager); | ||
|
||
mViewPager.setPageMargin(20); | ||
mViewPager.setOffscreenPageLimit(3); | ||
mViewPager.setAdapter(mAdapter = new PagerAdapter() | ||
{ | ||
@Override | ||
public Object instantiateItem(ViewGroup container, int position) | ||
{ | ||
ImageView view = new ImageView(MainActivity.this); | ||
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | ||
view.setLayoutParams(lp); | ||
// view.setText(position + ":" + view); | ||
view.setScaleType(ImageView.ScaleType.FIT_XY); | ||
// view.setBackgroundColor(Color.parseColor("#44ff0000")); | ||
view.setImageResource(imgRes[position]); | ||
container.addView(view); | ||
// view.setAdjustViewBounds(true); | ||
return view; | ||
} | ||
|
||
@Override | ||
public void destroyItem(ViewGroup container, int position, Object object) | ||
{ | ||
container.removeView((View) object); | ||
} | ||
|
||
@Override | ||
public int getCount() | ||
{ | ||
return imgRes.length; | ||
} | ||
|
||
@Override | ||
public boolean isViewFromObject(View view, Object o) | ||
{ | ||
return view == o; | ||
} | ||
}); | ||
|
||
|
||
mViewPager.setPageTransformer(true, new RotateDownPageTransformer()); | ||
|
||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) | ||
{ | ||
String[] effects = this.getResources().getStringArray(R.array.magic_effect); | ||
for (String effect : effects) | ||
menu.add(effect); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) | ||
{ | ||
String title = item.getTitle().toString(); | ||
mViewPager.setAdapter(mAdapter); | ||
|
||
if ("RotateDown".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new RotateDownPageTransformer()); | ||
} else if ("RotateUp".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new RotateUpPageTransformer()); | ||
} else if ("RotateY".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new RotateYTransformer()); | ||
} else if ("Standard".equals(title)) | ||
{ | ||
mViewPager.setClipChildren(false); | ||
mViewPager.setPageTransformer(true, NonPageTransformer.INSTANCE); | ||
} else if ("Alpha".equals(title)) | ||
{ | ||
mViewPager.setClipChildren(false); | ||
mViewPager.setPageTransformer(true, new AlphaPageTransformer()); | ||
} else if ("ScaleIn".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new ScaleInTransformer()); | ||
} else if ("RotateDown and Alpha".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer())); | ||
}else if ("RotateDown and Alpha And ScaleIn".equals(title)) | ||
{ | ||
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer(new ScaleInTransformer()))); | ||
} | ||
|
||
setTitle(title); | ||
|
||
return true; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
|
||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="160dp" | ||
android:clipChildren="false" | ||
android:layout_centerInParent="true" | ||
android:background="#aadc71ff" | ||
> | ||
<android.support.v4.view.ViewPager | ||
android:id="@+id/id_viewpager" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="60dp" | ||
android:layout_marginRight="60dp" | ||
android:layout_height="120dp" | ||
android:layout_gravity="center" | ||
> | ||
</android.support.v4.view.ViewPager> | ||
|
||
</FrameLayout> | ||
|
||
</RelativeLayout> |
Oops, something went wrong.