我正在研究Android的电子书阅读器。我在无滚动的webview中设置了我的内容。为了翻页,我想创建一种页面/拖动效果,如下所示: http://www.youtube.com/watch?v=sd5f4nauKTE#t=1m8s
我已经在我的webview上设置了一个onTouchListener,并将它的缓存图像添加到了ImageView,并设法用它的手指将它拖动,但我在路上设置动画时遇到了麻烦。
到目前为止,这是我的代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgContentBitmap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
<ImageView
android:id="@+id/imgNext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
<ImageView
android:id="@+id/imgPrev"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
<android.webkit.WebView
android:id="@+id/webPageContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.webkit.WebView>
</FrameLayout>
活动:
public class TestPageActivity extends Activity {
/** Called when the activity is first created. */
private WebView webView;
private Bitmap bitmap;
private ImageView contentBitmap;
private FrameLayout.LayoutParams params;
private int pressedX;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
params = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
params.leftMargin = 0;
params.topMargin = 0;
webView = (WebView) findViewById(R.id.webPageContent);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
((FrameLayout)(webView.getParent())).setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
int eventAction = event.getAction();
int currentX = (int) event.getX();
Log.d("main", "current x = "+currentX);
Log.d("main", "pressed x = "+pressedX);
switch(eventAction){
case MotionEvent.ACTION_DOWN:
Log.d("main", "down");
pressedX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
Log.d("main", "move");
params.leftMargin = (pressedX-currentX);
contentBitmap.setLayoutParams(params);
break;
case MotionEvent.ACTION_UP:
Log.d("main", "up");
break;
}
return true;
}
});
webView.loadData(getString(R.string.lorem_ipsum), "text/html", "utf-8");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
}
});
webView.setPictureListener(new PictureListener() {
@Override
public void onNewPicture(WebView view, Picture picture){
webView.setDrawingCacheEnabled(true);
webView.layout(0, 0, webView.getWidth(), webView.getBottom());
webView.buildDrawingCache(true);
bitmap = Bitmap.createBitmap(webView.getDrawingCache());
webView.setVisibility(WebView.INVISIBLE);
webView.setDrawingCacheEnabled(false);
contentBitmap.setImageBitmap(bitmap);
webView.setPictureListener(null);
}
});
contentBitmap = (ImageView) findViewById(R.id.imgContentBitmap);
/*
contentBitmap.setImageBitmap(bitmap);*/
}
}
答案 0 :(得分:1)
找到解决方案? 否则我会说,您可以将Fragment设置为视图,并使用FragmentManager和FragmentTransaction将视图放在Activity中。 之后,您可以使用以下命令向事件添加动画:setCustomAnimation(R.anim.New_in,R.anim.Old_out,R.anim.Old_in,R.anim.New_out);