Hi,
Iam currently working on picture viewer.Don't copy and paste this code just try to understand what is done and try to build from this code.
public class ImageViewer extends Activity {
ImageView iv;
ScrollView sv;
HorizontalScrollView hv;
LinearLayout lv;
RelativeLayout rl;
float zoom = 1.0f;
int angle=0;
public static Drawable img;
public static Bitmap resizedBitmap;
public static String f;
// private ArrayList<String> ImageList = new ArrayList<String>();
ImageButton bt1;
ImageButton bt2;
ImageButton bt3;
ImageButton bt4;
float scale=1.0f;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
int w;
int h;
int po;
String intent;
public String imagepath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_view);
// iv=(ImageView)findViewById(R.id.iv);
sv=(ScrollView)findViewById(R.id.scroll);
sv.layout(10,10,101,10);
//hv=(HorizontalScrollView)findViewById(R.id.hv);
rl=(RelativeLayout)findViewById(R.id.rl);
iv=new ImageView(ImageViewer.this);
iv.setVerticalScrollBarEnabled(true);
iv.setClickable(true);
rl.setGravity(Gravity.CENTER);
Intent i=getIntent();
po= i.getIntExtra("POSITION", 0);
intent=i.getStringExtra("FILE_PATH");
// ImageList =i.getStringArrayListExtra("ImageList");
System.out.println("The intent is"+intent);
bt1=(ImageButton)findViewById(R.id.image1);
bt2=(ImageButton)findViewById(R.id.image2);
bt3=(ImageButton)findViewById(R.id.image3);
bt4=(ImageButton)findViewById(R.id.image4);
Bitmap bitmapOrg = BitmapFactory.decodeFile(Gallary.path);
Matrix matrix = new Matrix();
matrix.postScale(scale,scale);
resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
//BitmapDrawable bmd=new BitmapDrawable(bitmapOrg);
// iv.setImageDrawable(bmd);
iv.setImageBitmap(resizedBitmap );
iv.setScaleType(ScaleType.CENTER);
sv.addView(iv);
sv.setVerticalScrollBarEnabled(true);
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
zoomIn();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
zoomOut();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
rotateLeft();
}
});
bt4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
rotateRight();
}
});
}
public void zoomIn()
{
//Bitmap bitmapOrg = BitmapFactory.decodeFile(f);
//int width = bitmapOrg.getWidth();
//int height = bitmapOrg.getHeight();
int width = ImageViewer.resizedBitmap.getWidth();
int height = ImageViewer.resizedBitmap.getHeight();
zoom=zoom+0.1f;
float scaleWidth = zoom;
float scaleHeight = zoom;
try{
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(angle);
// Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0, width, height, matrix, true);
Bitmap resizedBitmap = Bitmap.createBitmap(ImageViewer.resizedBitmap,0,0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
iv.setScaleType(ScaleType.FIT_CENTER);
// iv.layout(0, 0, 0, 0);
iv.setImageDrawable(bmd);
}
catch(final java.lang.OutOfMemoryError e)
{
System.gc();
}
}
public void zoomOut()
{
//Bitmap bitmapOrg = BitmapFactory.decodeFile(f);
//int width = bitmapOrg.getWidth();
// int height = bitmapOrg.getHeight();
zoom=zoom-0.1f;
if(zoom<=0.1f)
{
zoom=zoom+0.1f;
}
float scaleWidth = zoom;
float scaleHeight = zoom;
try{
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(angle);
// Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0, width, height, matrix, true);
Bitmap resizedBitmap = Bitmap.createBitmap(ImageViewer.resizedBitmap,0,0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
iv.setImageDrawable(bmd);
iv.setBackgroundColor(Color.WHITE);
iv.setScaleType(ScaleType.FIT_CENTER);
iv.setVerticalScrollBarEnabled(true);
iv.setHorizontalScrollBarEnabled(true);
}
catch(final java.lang.OutOfMemoryError e)
{
System.gc();
}
}
public void rotateLeft()
{
int width = ImageViewer.resizedBitmap.getWidth();
int height = ImageViewer.resizedBitmap.getHeight();
angle=angle+270;
float scaleWidth =zoom;
float scaleHeight = zoom;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(angle);
// Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
// width, height, matrix, true);
Bitmap resizedBitmap= Bitmap.createBitmap(ImageViewer.resizedBitmap, 0, 0,
width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
iv.setImageDrawable(bmd);
// iv.layout(0,0,0,0);
iv.setBackgroundColor(Color.WHITE);
iv.setScaleType(ScaleType.CENTER);
}
public void rotateRight()
{
int width = ImageViewer.resizedBitmap.getWidth();
int height = ImageViewer.resizedBitmap.getHeight();
angle=angle+90;
float scaleWidth = zoom;
float scaleHeight = zoom;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(angle);
Bitmap resizedBitmap= Bitmap.createBitmap(ImageViewer.resizedBitmap, 0, 0,
width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
iv.setImageDrawable(bmd);
iv.setBackgroundColor(Color.WHITE);
iv.setScaleType(ScaleType.CENTER);
}
}
[font=Arial Black] [/font] my applications functionalities are working fine...but with a small problem.....the image view is displaying contents in the top left corner of the window.....I added android:layout_gravity="center" to display the image view in the middle. but when the picture is zoomed the bottom and left sides are clippped....if any body give me some suggestions? [font=Arial] [/font]

