In this case, the picture has to be sized exactly [800x600], named like 'pic.jpg' or 'pic.png' and it should be placed in the folder 'res>drawable>' of your project.
Actually, making more complex views scrollable by hand should work the same way. However the coordinates of each component and its behavior have to be adapted after every single scroll movement. So if there's a way making it out of the box, that would be the preferable choice of course
Using java Syntax Highlighting
- package de.horizontalscroll;
- import de.horizontalscroll.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.*;
- import android.view.GestureDetector.OnGestureListener;
- import android.content.*;
- import android.graphics.*;
- import android.content.res.*;
- public class HorizontalScroll extends Activity implements OnGestureListener
- {
- private static final int X_MAX = 800;
- private static final int Y_MAX = 600;
- private int scrollX = 0;
- private int scrollY = 0;
- MyView main;
- Bitmap bmp;
- Bitmap adapt;
- Resources res;
- Paint paint;
- GestureDetector gestureScanner;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- gestureScanner = new GestureDetector(this);
- paint = new Paint();
- res = getResources();
- bmp = BitmapFactory.decodeResource(res, R.drawable.pic);
- adapt = Bitmap.createBitmap(bmp);
- main = new MyView(this);
- setContentView(main,new ViewGroup.LayoutParams(800,600));
- }
- @Override
- public boolean onTouchEvent(MotionEvent me)
- {
- return gestureScanner.onTouchEvent(me);
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
- {
- main.handleScroll(distanceX,distanceY);
- return true;
- }
- ////////////////////
- ///////////////////
- //////////////////
- @Override
- public boolean onDown(MotionEvent e)
- {
- return true;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
- {
- return true;
- }
- @Override
- public void onLongPress(MotionEvent e){ }
- @Override
- public void onShowPress(MotionEvent e) { }
- @Override
- public boolean onSingleTapUp(MotionEvent e)
- {
- return true;
- }
- ////////////////////
- ///////////////////
- //////////////////
- class MyView extends View
- {
- public MyView(Context context)
- {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- canvas.drawBitmap(adapt, 0, 0, paint);
- }
- public void handleScroll(float distX, float distY)
- {
- // X-Axis ////////////////////////////////
- if(distX > 6.0)
- {
- if(scrollX < 460)
- {
- scrollX += 15;
- }
- }
- else if(distX < -6.0)
- {
- if(scrollX >= 15)
- {
- scrollX -= 15;
- }
- }
- ////////////////////////////////////////////
- // Y-AXIS //////////////////////////////////
- if(distY > 6.0)
- {
- if(scrollY < 100)
- {
- scrollY += 15;
- }
- }
- else if(distY < -6.0)
- {
- if(scrollY >= 15)
- {
- scrollY -= 15;
- }
- }
- ////////////////////////////////////////////
- if((scrollX <= 480) && (scrollY <= 120))
- {
- adapt = Bitmap.createBitmap(bmp, scrollX, scrollY, 320, 480);
- invalidate();
- }
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4



.
