How to Add scroll bar ?

Put your problem here if it does not fit any of the other categories.

How to Add scroll bar ?

Postby androidUser » Wed Apr 22, 2009 7:32 am

How to Add scroll bar ? Could any one help.

Please find the below code.

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ZoomControls;

public class ImageScrolling extends Activity implements OnGestureListener {
//Zoom z;

private int scrollX = 0;
private int scrollY = 0;
private Drawable image;
private int zoomControler = 20;

MyView main;
Bitmap bmp;
Bitmap adapt;
Resources res;
Paint paint;
GestureDetector gestureScanner;
ZoomControls mZoom;

Intent intent = new Intent();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

gestureScanner = new GestureDetector(this);
paint = new Paint();

Resources res = getResources();

bmp = BitmapFactory.decodeResource(res, R.drawable.classic);
adapt = Bitmap.createBitmap(bmp);
image = this.getResources().getDrawable(R.drawable.classic);
main = new MyView(this);


setContentView(main, new ViewGroup.LayoutParams(800, 600));
}

@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {

main.handleScroll(distanceX, distanceY);

return true;
}

public boolean onDown(MotionEvent e) {
return true;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return true;
}

public void onLongPress(MotionEvent e) {
}

public void onShowPress(MotionEvent e) {
}

public boolean onSingleTapUp(MotionEvent e) {
return true;
}

class MyView extends View {
public MyView(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);


image.setBounds((getWidth() / 4) - zoomControler, (getHeight() / 4)
- zoomControler, (getWidth() / 4) + zoomControler,
(getHeight() / 4) + zoomControler);
image.draw(canvas);

canvas.drawBitmap(adapt, 0, 25, paint);

}
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_DPAD_UP)// zoom in
zoomControler += 10;
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
zoomControler -= 10;
if (zoomControler < 10) zoomControler = 10;

invalidate();
return true;
}


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 <= 500) && (scrollY <= 150))

{
adapt = Bitmap.createBitmap(bmp, scrollX, scrollY, 320, 480);
invalidate();
}

}
}


}
androidUser
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sun Apr 05, 2009 1:08 pm

Top

Postby alpesh_harsoda » Wed Apr 29, 2009 2:20 pm

For simple way to show the scorllbar is,
take one object of tablelayout and scrollview.
after that all the componets add to the tablelayout and at last add the tablelayout object to scrollview.
and setcontetview( object of scrollview).
thats it. :)
Regards,
Alpesh Patel,
Logistic Infotech
http://www.logisticinfotech.com
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby kris_xu » Wed May 06, 2009 8:18 am

how to?
kris_xu
Freshman
Freshman
 
Posts: 5
Joined: Fri Apr 03, 2009 3:42 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests