在这篇文章中,我们将带领您了解android–在ViewPager和setFillAfter上使用动画的全貌,包括android中viewpager的相关情况。同时,我们还将为您介绍有关Android
在这篇文章中,我们将带领您了解android – 在ViewPager和setFillAfter上使用动画的全貌,包括android中viewpager的相关情况。同时,我们还将为您介绍有关Android NestedScrollView 嵌套 ViewPager 以及 ViewPager 内嵌套 recyclerView 的问题、Android ParallaxViewPager:ViewPager背景视差Parallax移动、android ScrollView 嵌套ViewPager ,ViewPager又嵌套ScrollView,事件冲突、Android Viewpager和EditText的知识,以帮助您更好地理解这个主题。
本文目录一览:- android – 在ViewPager和setFillAfter上使用动画(android中viewpager)
- Android NestedScrollView 嵌套 ViewPager 以及 ViewPager 内嵌套 recyclerView 的问题
- Android ParallaxViewPager:ViewPager背景视差Parallax移动
- android ScrollView 嵌套ViewPager ,ViewPager又嵌套ScrollView,事件冲突
- Android Viewpager和EditText
android – 在ViewPager和setFillAfter上使用动画(android中viewpager)
我有一个ViewPager,我需要在按下按钮时整体移动.我用这个动画.
当我按它时,我翻译’x’.我使用setFillAfter(true)来保持新的位置.
但是当我更改ViewPager的页面时,它会跳回原来的x位置!
我只在Android 4.1上看到过这个问题,用Android 4.0没有问题!所以它看起来像Android中的某种回归.
我附上了一个测试项目,在那里我可以重现这个问题而不需要我周围的所有其他东西.我认为最好是帮助我解决这个问题,以便在Eclipse中导入项目并亲自查看.
我还添加了视频,一个在我的HTC One X上我看到了问题,而另一个在平板电脑上安装了Android 4.0,问题不在那里.
我一直在拼命想要解决这个丑陋的副作用,但直到现在还没有运气……
(对不起大电影文件…)
Video of Android 4.0 without the side effect
Video Android 4.1 with the side effect
the project where you can reproduce the issue with
编辑:
我添加了以下内容:
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) myViewPager.getLayoutParams();
if (!i)
lp.setMargins(300,0,0,0);
else
lp.setMargins(0,0,0,0);
myViewPager.setLayoutParams(lp);
}
之后它会保持在正确的位置,但它会快速闪烁,就像动画在最后仍然显示一样,当我更改边距时,它仍会显示动画后的偏移量.然后它跳到正确的位置.
解决方法:
主要问题似乎是动画类型的选择不正确.您会看到,View Animation作为工具不适用于ViewPager等复杂的交互式对象.它仅提供绘图场所的低成本动画.动画ViewPager响应用户动作的视觉行为是未定义的,不应该依赖它.
丑陋的电影,当你用真实物体代替“gost”时,这是很自然的.
自API 11以来打算在您的案例中使用的机制是构建在Views中以优化性能的专用属性动画:ViewPropertyAnimator,或者不是专门的,但是更通用的ObjectAnimator和AnimatorSet.
Property animation使View真正改变了它的位置和功能.
要制作项目,使用ViewPropertyAnimator,请将您的侦听器设置更改为:
btn.setonClickListener(new OnClickListener() {
boolean b = false;
@Override
public void onClick(View v) {
if(b) {
myViewPager.animate().translationX(0f).setDuration(700);
}
else {
myViewPager.animate().translationX(300f).setDuration(700);
}
b=!b;
}
});
如果只想使用xml配置,请坚持使用| ObjectAnimator和AnimatorSet.请仔细阅读以上链接以获取更多信息.
如果你急于支持预蜂窝设备,你可以使用Jake Warton的NineOldAndroids项目.希望有所帮助.
Android NestedScrollView 嵌套 ViewPager 以及 ViewPager 内嵌套 recyclerView 的问题
NestedScrollView 嵌套 viewpager 会出现两个问题
1.viewpager 无法显示,高度为 0
2. 嵌套后 NestedScrollView 无法滑动
先说第一个问题:
viewpager 无法正常显示,只需要在 NestedScrollView 内加入
android:fillViewport="true"
就可以了
<android.support.v4.widget.NestedScrollView
android:id="@+id/novice_live_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:fillViewport="true"
>
第二个问题,嵌套后 NestedScrollView 无法滑动,这个问题只要我们重写一个 viewpager,然后在 xml 里用我们的 viewpager 就可以了
public class WrapContentHeightViewPager extends ViewPager {
public WrapContentHeightViewPager(Context context) {
super(context);
}
public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
如果 viewpager 内还嵌套了 recycleview,只想让 nestedScrollView 滑动,recyclerView 只用来展示,那么还需要对 recyclerview 进行处理,如下:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
@Override
public boolean canScrollVertically() {
return false;
}
};
recycler.setLayoutManager(linearLayoutManager);
记录一个发现的小坑,有遇到这些问题的朋友,希望这个帖子可以帮到大家。
Android ParallaxViewPager:ViewPager背景视差Parallax移动
Android ParallaxViewPager:ViewPager背景视差Parallax移动
附录的相关文章,实现了一种是当ViewPager左右滑动时候,背景伴随左右滑动,附录的那一篇文章中介绍的BackgroundViewPager从一定意义上讲是把ViewPager的背景图片n等均分,每一个ViewPager页面均分得到1/n宽度的背景图片内容。
而本文要介绍的Android ParallaxViewPager不同于BackgroundViewPager,Android ParallaxViewPager实现一种在Android ViewPager页面左右翻动时候特殊的视察移动的视觉效果,如下面的动态图所示:
测试的主MainActivity.java:
package zhangphil.viewpager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParallaxViewPager parallaxViewPager = ((ParallaxViewPager) findViewById(R.id.viewpager));
parallaxViewPager.setOverlapPercentage(0.75f);
parallaxViewPager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {
@Override
public Fragment getItem(final int pos) {
// 测试的Fragment
Fragment f = new Fragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) v.findViewById(android.R.id.text1);
text.setText("Zhang Phil " + pos);
text.setGravity(Gravity.CENTER);
text.setTextColor(Color.RED);
text.setTextSize(50f);
return v;
}
};
return f;
}
// 假设有5个页面
@Override
public int getCount() {
return 5;
}
});
}
}
把ParallaxViewPager当作一个和Android标准的ViewPager一样使用、写进布局文件。
布局文件:
<RelativeLayout 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"
tools:context="zhangphil.viewpager.MainActivity" >
<zhangphil.viewpager.ParallaxViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" />
</RelativeLayout>
注意要衬一张较宽的图片作为ParallaxViewPager的背景,因为通常ViewPager是加载多个页面的,这样左右翻动可以有充足的宽度跟随ViewPager左右滑动。
本例的背景图片background.jpg:
核心的ParallaxViewPager.java:
package zhangphil.viewpager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
@SuppressLint("NewApi")
public class ParallaxViewPager extends ViewPager {
public static final int FIT_WIDTH = 0;
public static final int FIT_HEIGHT = 1;
public static final float OVERLAP_FULL = 1f;
public static final float OVERLAP_HALF = 0.5f;
public static final float OVERLAP_QUARTER = 0.25f;
private static final float CORRECTION_PERCENTAGE = 0.01f;
public Bitmap bitmap;
private Rect source, destination;
private int scaleType;
private int chunkWidth;
private int projectedWidth;
private float overlap;
private OnPageChangeListener secondOnPageChangeListener;
public ParallaxViewPager(Context context) {
super(context);
init();
}
public ParallaxViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
source = new Rect();
destination = new Rect();
scaleType = FIT_HEIGHT;
overlap = OVERLAP_HALF;
setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (bitmap != null) {
source.left = (int) Math.floor((position + positionOffset - CORRECTION_PERCENTAGE) * chunkWidth);
source.right = (int) Math
.ceil((position + positionOffset + CORRECTION_PERCENTAGE) * chunkWidth + projectedWidth);
destination.left = (int) Math
.floor((position + positionOffset - CORRECTION_PERCENTAGE) * getWidth());
destination.right = (int) Math
.ceil((position + positionOffset + 1 + CORRECTION_PERCENTAGE) * getWidth());
invalidate();
}
if (secondOnPageChangeListener != null) {
secondOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
}
@Override
public void onPageSelected(int position) {
if (secondOnPageChangeListener != null) {
secondOnPageChangeListener.onPageSelected(position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
if (secondOnPageChangeListener != null) {
secondOnPageChangeListener.onPageScrollStateChanged(state);
}
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
destination.top = 0;
destination.bottom = h;
if (getAdapter() != null && bitmap != null)
calculateParallaxParameters();
}
private void calculateParallaxParameters() {
if (bitmap.getWidth() < getWidth() && bitmap.getWidth() < bitmap.getHeight() && scaleType == FIT_HEIGHT) {
Log.w(ParallaxViewPager.class.getName(),
"Invalid bitmap bounds for the current device, parallax effect will not work.");
}
final float ratio = (float) getHeight() / bitmap.getHeight();
if (ratio != 1) {
switch (scaleType) {
case FIT_WIDTH:
source.top = (int) ((bitmap.getHeight() - bitmap.getHeight() / ratio) / 2);
source.bottom = bitmap.getHeight() - source.top;
chunkWidth = (int) Math.ceil((float) bitmap.getWidth() / (float) getAdapter().getCount());
projectedWidth = chunkWidth;
break;
case FIT_HEIGHT:
default:
source.top = 0;
source.bottom = bitmap.getHeight();
projectedWidth = (int) Math.ceil(getWidth() / ratio);
chunkWidth = (int) Math
.ceil((bitmap.getWidth() - projectedWidth) / (float) getAdapter().getCount() * overlap);
break;
}
}
}
/**
* Sets the background from a resource file.
*
* @param resid
*/
@Override
public void setBackgroundResource(int resid) {
bitmap = BitmapFactory.decodeResource(getResources(), resid);
}
/**
* Sets the background from a Drawable.
*
* @param background
*/
@Override
public void setBackground(Drawable background) {
bitmap = ((BitmapDrawable) background).getBitmap();
}
/**
* Deprecated. Sets the background from a Drawable.
*
* @param background
*/
@Override
public void setBackgroundDrawable(Drawable background) {
bitmap = ((BitmapDrawable) background).getBitmap();
}
/**
* Sets the background from a bitmap.
*
* @param bitmap
* @return The ParallaxViewPager object itself.
*/
public ParallaxViewPager setBackground(Bitmap bitmap) {
this.bitmap = bitmap;
return this;
}
/**
* Sets how the view should scale the background. The available choices are:
* <ul>
* <li>FIT_HEIGHT - the height of the image is resized to matched the height
* of the View, also stretching the width to keep the aspect ratio. The
* non-visible part of the bitmap is divided into equal parts, each of them
* sliding in at the proper position.</li>
* <li>FIT_WIDTH - the width of the background image is divided into equal
* chunks, each taking up the whole width of the screen.</li>
* </ul>
*
* @param scaleType
* @return
*/
public ParallaxViewPager setScaleType(final int scaleType) {
if (scaleType != FIT_WIDTH && scaleType != FIT_HEIGHT)
throw new IllegalArgumentException("Illegal argument: scaleType must be FIT_WIDTH or FIT_HEIGHT");
this.scaleType = scaleType;
return this;
}
/**
* Sets the amount of overlapping with the setOverlapPercentage(final float
* percentage) method. This is a number between 0 and 1, the smaller it is,
* the slower is the background scrolling.
*
* @param percentage
* @return The ParallaxViewPager object itself.
*/
public ParallaxViewPager setOverlapPercentage(final float percentage) {
if (percentage <= 0 || percentage >= 1)
throw new IllegalArgumentException("Illegal argument: percentage must be between 0 and 1");
overlap = percentage;
return this;
}
/**
* Recalculates the parameters of the parallax effect, useful after changes
* in runtime.
*
* @return The ParallaxViewPager object itself.
*/
public ParallaxViewPager invalidateParallaxParameters() {
calculateParallaxParameters();
return this;
}
@Override
protected void onDraw(Canvas canvas) {
if (bitmap != null)
canvas.drawBitmap(bitmap, source, destination, null);
}
public void addOnPageChangeListener(OnPageChangeListener listener) {
secondOnPageChangeListener = listener;
}
}
代码运行结果如上面的动态图所示。
Android ParallaxViewPager在github上的主页是:https://github.com/andraskindler/parallaxviewpager
附录相关文章:
《Android BackgroundViewPager:类似桌面背景壁纸随手指滑动》链接地址:http://blog.csdn.net/zhangphil/article/details/50347571
android ScrollView 嵌套ViewPager ,ViewPager又嵌套ScrollView,事件冲突
android
开发工具:android studio
问题:实现一个类似淘宝之前的一个上拉查看商品详情功能,但是我们这个嵌套关系是这样的,ScrollView ——>LinearLayout——>ViewPager——>ScrollView,现在问题是,内层ScrollView滚动到顶时无法将事件传递给外层的ScrollView,导致整个视图滑上去之后就滑不下来了,求大神给个能解决这个问题的demo,不胜感激。
Android Viewpager和EditText
我在Activity里面做了一个ViewPager的一个3s轮播的图片浏览,下面是EditText的评论输入框,但是这两个毫不相干的东西在一个界面里,会影响到EditText的焦点,而且还会改变软键盘的输出状态(中文输出变成了英文输出)?
问题:当viewpager轮播到下一个的时候EditText竟会失去焦点,软键盘改为英文输出
首先我尝试过AndroidManifest里面设置
android:windowSoftInputMode="adjustUnspecified|stateHidden|adjustPan">其次,用
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
控制软键盘显示EditText.requestFocus()获取焦点都没有好的效果。
急急急~求大神帮忙
关于android – 在ViewPager和setFillAfter上使用动画和android中viewpager的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Android NestedScrollView 嵌套 ViewPager 以及 ViewPager 内嵌套 recyclerView 的问题、Android ParallaxViewPager:ViewPager背景视差Parallax移动、android ScrollView 嵌套ViewPager ,ViewPager又嵌套ScrollView,事件冲突、Android Viewpager和EditText的相关信息,请在本站寻找。
本文标签: