- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个用于水平滑动的全屏图像 slider 的 viewpager。我想做的是在 slider 上的每个图像后面放一个图像。也就是说,用户可以点击“翻转”按钮来显示另一张图片。
下面是我的代码,但是我有两个问题:
注意:我使用的是 fullscreenActivity 主题,它会隐藏 UI 控件,直到用户单击图像。
FullscreenActivity.java
package me.zamaaan.wallpaper;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import me.zamaaan.wallpaper.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import static me.zamaaan.wallpaper.HeavyLifter.FAIL;
import static me.zamaaan.wallpaper.HeavyLifter.SUCCESS;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import java.util.ArrayList;
import java.util.List;
/** A helper class that will do the heavy work of decoding images and actually setting the wallpaper */
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class FullscreenActivity extends Activity implements OnClickListener, AnimationListener{
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
private HeavyLifter chuckNorris;
private Animation animation1;
private Animation animation2;
private boolean isBackOfCardShowing = true;
/*
an array with the ids of the images for the viewpager
*/
public Integer[] mImageIds = {
R.drawable.background1, R.drawable.background2,
R.drawable.background3, R.drawable.background4,
R.drawable.background1, R.drawable.background2,
R.drawable.background1, R.drawable.background2,
R.drawable.background3, R.drawable.background4,
R.drawable.background1, R.drawable.background2,
R.drawable.background3
};
/*
an array with the ids of the images for the back flip of each image in the viewpager
*/
public Integer[] mBackIds = {
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash, R.drawable.splash,
R.drawable.splash
};
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {@link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {@link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
animation1.setAnimationListener(this);
animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
animation2.setAnimationListener(this);
findViewById(R.id.flip).setOnClickListener(this);
// Loop through the ids to create a list of full screen image views
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.view_pager);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.btnSetAsWallpaper).setOnTouchListener(
mDelayHideTouchListener);
findViewById(R.id.btnSaveWallpaper).setOnTouchListener(
mDelayHideTouchListener);
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
//set title of Image
this.setTitle(imageAdapter.mThumbTitles[position]);
// Load are heavy lifter (goes and does work on another thread), to get a response after the lifters thread
// has finished we pass in a Handler that will be notified when it completes
chuckNorris = new HeavyLifter(this, chuckFinishedHandler);
}
@Override
public void onClick(View v) {
v.setEnabled(false);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).clearAnimation();
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).setAnimation(animation1);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).startAnimation(animation1);
}
@Override
public void onAnimationEnd(Animation animation) {
int i = getIntent().getExtras().getInt("id");
if (animation==animation1) {
if (isBackOfCardShowing) {
((ImageView)findViewById(i)).setImageResource(mImageIds[i]);
} else {
((ImageView)findViewById(i)).setImageResource(mBackIds[i]);
}
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).clearAnimation();
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).setAnimation(animation2);
((ImageView)findViewById(getIntent().getExtras().getInt("id"))).startAnimation(animation2);
} else {
isBackOfCardShowing=!isBackOfCardShowing;
findViewById(R.id.flip).setEnabled(true);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
/**
* Called from XML when the save wallpaper button is pressed
* Thie retrieves the id of the current image from our list
* It then asks chuck to save it as a wallpaper!
* The chuckHandler will be called when this operation is complete
* @param v
*/
public void saveWallpaper(View v) {
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
// Show a toast message on successful save
if(chuckNorris.saveResourceAsWallpaper(imageAdapter.mThumbIds[position])){
Toast.makeText(FullscreenActivity.this, "Image Saved to ZamaaanWallpaper folder", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(FullscreenActivity.this, "Image couldn't be saved, try again", Toast.LENGTH_SHORT).show();
}
}
/**
* Called from XML when the set wallpaper button is pressed
* Thie retrieves the id of the current image from our list
* It then asks chuck to set it as a wallpaper!
* The chuckHandler will be called when this operation is complete
* @param v
*/
public void setAsWallpaper(View v) {
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
chuckNorris.setResourceAsWallpaper(imageAdapter.mThumbIds[position]);
}
/**
* This is the handler that is notified when are HeavyLifter is finished doing an operation
*/
private Handler chuckFinishedHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SUCCESS:
Toast.makeText(FullscreenActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
break;
case FAIL:
Toast.makeText(FullscreenActivity.this, "Wallper NOT set, try again", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
};
}
最佳答案
我解决了我的问题。我只需要:
boolean isBackOfCardShowing
更改为 boolean 数组并将其初始化为 false。viewpager.getCurrentItem()
获取当前页面的位置,而不是我正在做的。关于java - 在 viewpager 中翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21486298/
我目前面临将 ViewPager 放入另一个 ViewPager 中的问题,两者都可滑动。我成功创建了我的第一个 ViewPager,它包含三个页面,中间一个是第二个(内部)ViewPager,其中包
在我的主要 Activity 中,我有一个水平 viewpager。在 viewpager 的一个 fragment 中,我有另一个垂直 viewpager。两者都很好。但是对于水平 viewpage
我想创建一个 ViewPager(包含三个项目),其中每个 View 都是另一个 ViewPager(包含两个项目)。用户然后像这样滑动项目: ViewPager1[0] ViewPager2[0]
如何修复这个错误 setupWithViewPager(android.support.v4.view.ViewPager) in TabLayout cannot br applied to (an
我有一个包含 3 张图片的数组的 viewPage,我想在缩放其中一张时锁定 viewPager,因为当我在右侧滚动时,会出现下一张照片!这是我的代码 enter code here public
我是第一次实现 ViewPager,我遇到了一些问题,因为我收到以下错误: 06-20 10:40:51.366 11377-11377/com.example.ruelas.elite E/Andr
我真的是 android 的新手,如果对我的类(class)作业有任何帮助,我将不胜感激。 我需要做的: 1) 在一个 Activity 中有两个 ViewPagers(未嵌套) 2)两个ViewPa
我正在自定义我的 Android 应用程序以在四个方向上滑动,我正在使用 ViewPager 水平 和 VerticalViewPager 滑动垂直滑动(这就像对 DirectionalViewPag
我需要为平板电脑创建一个 ViewPager,在平板电脑的左侧会有导航按钮供用户选择。例如。 左侧导航将有 4 个图像按钮,每个项目将用户带到不同的教程。每个图像按钮都会加载一个 ViewPager。
我用过Vertical Viewpager在我的显示数据集合的项目中。在单个数据(项目)中,我有更多图像要显示。所以我尝试使用 viewpager。 但是当我水平滚动时,它会阻止垂直 Viewpage
我有一个父级 ViewPager,它的每个页面都包含一个子级 ViewPager。子 ViewPager 可能包含一个 ListView 或一个垂直的 ScrollView。我想将子级 ViewPag
我的任务是创建一个非常不正统的布局,只能通过另一个 viewpager 的第二个 Pane 内的 viewpager 来完成,幸运的是外部 viewpager 需要被锁定所以任务更现实一些,但是意外的
目标是根据屏幕方向(纵向或横向)以不同的布局在屏幕上显示两个 fragment ( fragment A、 fragment B)。 当设备处于纵向模式时,一次仅显示 Frag A 和 Frag B
我想禁用父 viewpager 中的子 viewpager 的滑动。 我目前使用这个自定义子 viewpager public class CustomViewPager extends ViewPa
我有两个 ViewPager - 一个在另一个下面。现在客户希望我们添加扩展底部 ViewPager(具有三个 ListView)以覆盖顶部 ViewPager 的可能性。当我将两个 ViewPage
我用谷歌搜索了这个问题很多天。找到一个解决方案,在Viewpager的开头和结尾添加一个空白View。当viewpager.getcurrentItem()==0时,则重新指向1。在我看来这种做法非常
我从适配器设置标题后,标题未出现在 PagerTabStrip 中 这是我的 Activity public class MainActivity extends AppCompatActivity
我有一个 ViewPager,其中包含多个 TextView,它们具有不同的字体大小。此外,我还获得了增加/减少字体大小的按钮,通过添加其默认大小加上一个名为 STEP 的值(该值通过 inc/dec
我有一个嵌套的 ViewPager,效果非常好。唯一的问题是,一旦子 ViewPager 位于最后一项并且我进一步滚动,父 ViewPager 就会滚动。我不想要这种行为。 我如何实现这一目标? 这是
我想显示一个带有滑动抽屉的 Viewpager。Viewpager 用于滚动图像,我想在该 viewpager 的顶部放置一个滑动抽屉(从顶部到按钮)。 我附上了我的示例代码。如果您对如何实现此屏幕有
我是一名优秀的程序员,十分优秀!