我正在尝试在harism页面上添加缩放手势curl libray图像
https://github.com/harism/android_page_curl/
我有覆盖onTouchEvent(MotionEvent event)
并创建ScaleGestureDetector.OnScaleGestureListener
的实例以检测缩放事件。此部分已成功实现,如我的示例代码中的(1) simpleOnScaleGestureListener
类所示。现在我需要根据缩放比例绘制我的位图,但我无法将其实现为日志显示(2) java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
。有人可以帮我解决这个错误。
1
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lp = this;
util = new Utils();
Prefs = new PreferencesHelper("flag",lp);
PrefsPortrait = new PreferencesHelper("pflag",lp);
//llFooterPageOne = (LinearLayout) findViewById(R.id.llFooterPageOne);
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
if (getLastNonConfigurationInstance() != null) {
index = (Integer) getLastNonConfigurationInstance();
}
mCurlView = (CurlView) findViewById(R.id.curl);
mCurlView.setPageProvider(new PageProvider());
mCurlView.setSizeChangedObserver(new SizeChangedObserver());
mCurlView.setCurrentIndex(index);
// mCurlView.setBackgroundResource(R.drawable.icon);
imHome = (ImageView)findViewById(R.id.imHome);
imHome.setClickable(true);
imHome.setOnClickListener(lp);
btOne=(Button)findViewById(R.id.btOne);
btTwo=(Button)findViewById(R.id.btTwo);
btThree=(Button)findViewById(R.id.btThree);
btFour=(Button)findViewById(R.id.btFour);
llPageOne = (LinearLayout)findViewById(R.id.llPageOne);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
btThree.setOnClickListener(this);
btFour.setOnClickListener(this);
// This is something somewhat experimental. Before uncommenting next
// line, please see method comments in CurlView.
// mCurlView.setEnableTouchPressure(true);
}
@Override
public void onPause() {
super.onPause();
mCurlView.onPause();
}
@Override
public void onResume() {
super.onResume();
mCurlView.onResume();
System.out.println("index : "+ index);
}
@Override
public Object onRetainNonConfigurationInstance() {
return mCurlView.getCurrentIndex();
}
/**
* Bitmap provider.
*/
private class PageProvider implements CurlView.PageProvider {
// Bitmap resources.
private int[] mBitmapIds = {
R.drawable.image_one, R.drawable.image_two, R.drawable.image_three
};
@Override
public int getPageCount() {
//return 5;
pagesCount = 0;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int wwidth = displaymetrics.widthPixels;
int hheight = displaymetrics.heightPixels;
if(wwidth > hheight){
if((mBitmapIds.length % 2) > 0)
pagesCount = (mBitmapIds.length / 2) + 1;
else
pagesCount = mBitmapIds.length / 2;
}else{
pagesCount = mBitmapIds.length;
}
System.out.println("page count "+pagesCount);
return pagesCount;
}
private Bitmap loadBitmap(int width, int height, int index) {
bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.eraseColor(0xFFFFFFFF);
canvas = new Canvas(bitmap);
drawable = getResources().getDrawable(mBitmapIds[index]);
System.out.println("canvas width: "+canvas.getWidth());
int margin = 3;//7
int border = 3;//2
Rect r = new Rect(margin, margin, width - margin, height - margin);
int imageWidth = r.width() - (border * 2);
int imageHeight = imageWidth * drawable.getIntrinsicHeight()
/ drawable.getIntrinsicWidth();
if (imageHeight > r.height() - (border * 2)) {
imageHeight = r.height() - (border * 2);
imageWidth = imageHeight * drawable.getIntrinsicWidth()
/ drawable.getIntrinsicHeight();
}
Log.d("TAG", String.valueOf(imageHeight));
if (lp.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
r.left += ((r.width() - imageWidth) / 2) - border;
r.right = r.left + imageWidth + border + border;
} else if (lp.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int wwidth = displaymetrics.widthPixels;
int hheight = displaymetrics.heightPixels;
int mask = Configuration.SCREENLAYOUT_SIZE_MASK;
int size = lp.getResources().getConfiguration().screenLayout;
if ((size & mask) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
// Screen is classified x-large
r.left += ((r.width() - imageWidth)) - border-126;
r.right = r.left + imageWidth + border + border+126;
}else{
if(wwidth > hheight){
r.left += ((r.width() - imageWidth)) - border-126;
r.right = r.left + imageWidth + border + border+126;
}else {
r.left += ((r.width() - imageWidth)) - border;
r.right = r.left + imageWidth + border + border;
}
}
}
System.out.println("getActivity().getResources().getConfiguration().orientation");
r.top += ((r.height() - imageHeight) / 2) - border;
r.bottom = r.top + imageHeight + border + border;
Paint p = new Paint();
p.setColor(0xFFFFFFFF);
canvas.drawRect(r, p);
r.left += border;
r.right -= border;
r.top += border;
r.bottom -= border;
drawable.setBounds(r);
drawable.draw(canvas);
return bitmap;
}
@Override
public void updatePage(CurlPage page, int width, int height, int index) {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int wwidth = displaymetrics.widthPixels;
int hheight = displaymetrics.heightPixels;
if(wwidth > hheight){
if (index >0){
front = loadBitmap(width, height, (index*2));
back = loadBitmap(width, height, (index*2)+1);
} else {
front = loadBitmap(width, height, (index));
back = loadBitmap(width, height, (index));
}
matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);
mirroredBitmap = Bitmap.createBitmap(back, 0, 0, back.getWidth() , back.getHeight(), matrix, false);
page.setTexture(front, CurlPage.SIDE_FRONT);
page.setTexture(mirroredBitmap, CurlPage.SIDE_BACK);
Prefs.SavePreferences("flag", "1");
PrefsPortrait.SavePreferences("pflag", "");
} else {
//-----
System.out.println("current case portrait orientation... "+flag);
if (Prefs.GetPreferences("flag").equalsIgnoreCase("1")){
if (index>1){
index = index*2;
}
Prefs.SavePreferences("flag", "");
}
PrefsPortrait.SavePreferences("pflag", "1");
front = loadBitmap(width, height, index);
back = loadBitmap(width, height, index);
System.out.println("index case"+index);
page.setTexture(front, CurlPage.SIDE_FRONT);
page.setTexture(back, CurlPage.SIDE_BACK);
}
}
}
/**
* CurlView size changed observer.
*/
private class SizeChangedObserver implements CurlView.SizeChangedObserver {
@Override
public void onSizeChanged(int w, int h) {
if (w > h) {
mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);
mCurlView.setMargins(.000f, .000f, .000f, .000f);
} else {
mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
mCurlView.setMargins(.005f, .005f, .00f, .00f);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imHome:
mCurlView.setCurrentIndex(0);
System.out.println("home pressed");
mCurlView.onResume();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drop_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_option_one:
lp.startActivity(new Intent(lp,ContactForm.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
// scaleGesture.setText(String.valueOf(detector.getScaleFactor()));
counter =counter +1;
int bitwidth = back.getWidth()+counter;
int bitheight = back.getHeight()+counter;
if (counter==200){
counter = 0;
bitwidth = bitwidth -200;
bitheight = bitheight - 200;
}
System.out.println("Test Plan "+counter);
mirroredBitmap = Bitmap.createBitmap(back, 0, 0, bitwidth , bitheight, matrix, true);
mCurlView.onResume();
//mirroredBitmap = Bitmap.createBitmap(back, 0, 0, back.getWidth()+counter , back.getHeight()+counter , matrix, false);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
// scaleGesture.setVisibility(View.VISIBLE);
System.out.println("Mad Plan");
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
// scaleGesture.setVisibility(View.INVISIBLE);
System.out.println("Mad Plan");
}
}
2
12-09 14:26:02.770: E/AndroidRuntime(7470): java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.graphics.Bitmap.createBitmap(Bitmap.java:533)
12-09 14:26:02.770: E/AndroidRuntime(7470): at projectname.LPMagHome$simpleOnScaleGestureListener.onScale(LPMagHome.java:620)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ScaleGestureDetector.onTouchEvent(ScaleGestureDetector.java:300)
12-09 14:26:02.770: E/AndroidRuntime(7470): at projectname.LPMagHome.onTouchEvent(LPMagHome.java:592)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
12-09 14:26:02.770: E/AndroidRuntime(7470): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.View.dispatchPointerEvent(View.java:7520)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4452)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4503)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.Choreographer.doFrame(Choreographer.java:523)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.os.Handler.handleCallback(Handler.java:615)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.os.Looper.loop(Looper.java:137)
12-09 14:26:02.770: E/AndroidRuntime(7470): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-09 14:26:02.770: E/AndroidRuntime(7470): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 14:26:02.770: E/AndroidRuntime(7470): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 14:26:02.770: E/AndroidRuntime(7470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-09 14:26:02.770: E/AndroidRuntime(7470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-09 14:26:02.770: E/AndroidRuntime(7470): at dalvik.system.NativeStart.main(Native Method)
更新我在触摸类上找到了这个,我如何将它应用到我的活动中。请帮助
public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
private static final float MIN_ZOOM = 1.0f;
private static final float MAX_ZOOM = 5.0f;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.image);
//view.setLayoutParams(new GridView.LayoutParams(85, 85));
// view.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: //first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
Log.i("saran","mode === drag");
break;
case MotionEvent.ACTION_UP: //first finger lifted
Log.i("saran","action up");
case MotionEvent.ACTION_POINTER_UP: //second finger lifted
mode = NONE;
Log.i("saran", "mode=NONE" );
break;
case MotionEvent.ACTION_POINTER_DOWN: //second finger down
oldDist = spacing(event);
Log.i("saran", "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.i("saran", "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) { //movement of first finger
matrix.set(savedMatrix);
if (view.getLeft() >= -392){
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
Log.i("saran", "action move.....");
}
else if (mode == ZOOM) { //pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f) {
matrix.set(savedMatrix);
scale = newDist / oldDist; //**//thinking i need to play around with this value to limit it**
matrix.postScale(scale, scale, mid.x, mid.y);
}
Log.i("saran", "zoom.......");
}
break;
}
// Perform the transformation
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
Log.d(TAG, sb.toString());
}
}
答案 0 :(得分:0)
尝试此处在此处添加任何数字以进行缩放
int imageWidth = r.width() - (border * 2) + **30**;
我添加了30个结果。
我希望它对你有帮助。
答案 1 :(得分:0)
我通过将边距设置为负值来玩下面的代码。
private class SizeChangedObserver implements CurlView.SizeChangedObserver {
@Override
public void onSizeChanged(int w, int h) {
if (w > h) {
mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);
mCurlView.setMargins(-.5f, -.5f, -.5f, -.5f);//Zoom in
//mCurlView.setMargins(.01f, .01f, .01f, .01f); //zoom Out
} else {
mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
mCurlView.setMargins(.01f, .01f, .01f, .01f);
}
}
}