package
com.imageopen;
import
android.app.Activity;
import
android.graphics.Bitmap;
import
android.os.Bundle;
import
android.util.DisplayMetrics;
import
android.util.Log;
import
android.view.Menu;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.View.OnTouchListener;
import
android.view.Window;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.ImageView.ScaleType;
public
class
BigzoonImage
extends
Activity {
ImageView myImageView;
Button bigButton;
Button smallButton;
View myButtons;
private
Bitmap myBitmap;
private
double
bigSize =
1.25
;
private
double
smallSize =
0.8
;
double
size =
1
;
double
pixel =
30.00
;
int
bmpWidth;
int
bmpHight;
int
bmpSizeWidth;
int
bmpSizeHight;
int
x ;
int
y ;
int
screenWidth;
int
screenHeight;
int
dstHeight;
int
dstWidth;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super
.onCreate(savedInstanceState);
setContentView(R.layout.bigzoonimage_main);
init();
}
private
void
init(){
myImageView = (ImageView)findViewById(R.id.bitmap_image);
bigButton = (Button)findViewById(R.id.button_big);
smallButton = (Button)findViewById(R.id.button_small);
myButtons = (View)findViewById(R.id.bitmap_button);
MyBitmapFactory myBitmapFactory =
new
MyBitmapFactory(
this
);
myBitmap = myBitmapFactory.getDrawBmp(R.drawable.bitmap_test);
myImageView.setImageBitmap(myBitmap);
myImageView.setOnTouchListener(ImageOpenListener);
bigButton.setOnClickListener(sizeButton);
smallButton.setOnClickListener(sizeButton);
DisplayMetrics dm =
new
DisplayMetrics();
dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
private
void
big(){
size = bigSize * size;
Bitmap newBitmap = myBitmap;
newBitmap = bigCal(myBitmap);
newBitmap = MyBitMap.createScaledBitmap(newBitmap, dstWidth, dstHeight, ScaleType.FIT_XY);
myImageView.setImageBitmap(newBitmap);
}
private
void
small(){
size = smallSize * size;
Bitmap newBitmap = myBitmap;
newBitmap = bigCal(newBitmap);
newBitmap = MyBitMap.createScaledBitmap(newBitmap, dstWidth, dstHeight, ScaleType.FIT_XY);
myImageView.setImageBitmap(newBitmap);
}
private
OnClickListener sizeButton =
new
OnClickListener() {
@Override
public
void
onClick(View v) {
if
(v == bigButton){
big();
}
else
small();
}
};
public
Bitmap bigCal(Bitmap bitmap){
bmpWidth = bitmap.getWidth();
bmpHight = bitmap.getHeight();
int
sizeMax = Math.min(myBitmap.getWidth()/
16
, myBitmap.getHeight()/
16
);
double
sizeMin = Math.max(pixel/myBitmap.getWidth(), pixel/myBitmap.getHeight());
if
(size > sizeMax){
size = sizeMax;
}
if
(size < sizeMin){
size = sizeMin;
}
bmpSizeWidth = (
int
)(bmpWidth*size);
bmpSizeHight = (
int
)(bmpHight*size);
if
(screenWidth > bmpSizeWidth){
x =
0
;
dstWidth = bmpSizeWidth;
}
else
{
x = (
int
)((bmpSizeWidth - screenWidth)/(
2
*size));
bmpWidth = (
int
)(screenWidth / size);
dstWidth = screenWidth;
}
if
(screenHeight > bmpSizeHight){
y =
0
;
dstHeight = bmpSizeHight;
}
else
{
y = (
int
)((bmpSizeHight - screenHeight)/(
2
*size));
bmpHight = (
int
)(screenHeight / size);
dstHeight = screenHeight;
}
bitmap = Bitmap.createBitmap(bitmap, x, y, bmpWidth, bmpHight);
return
bitmap;
}
public
double
rowOrCowNum(
int
sizeBitmapWH,
int
screenWH){
double
num = (sizeBitmapWH * size)/screenWH;
return
num;
}
public
Bitmap movCal(Bitmap bitmap,
int
dx,
int
dy){
double
coordinateX = rowOrCowNum(myBitmap.getWidth() , screenWidth);
double
coordinateY = rowOrCowNum(myBitmap.getHeight(), screenHeight);
if
(coordinateX >
1
){
if
(dx >
0
){
x -= (myBitmap.getWidth()/(coordinateX *
4
));
if
(x <
0
){
x =
0
;
}
}
if
(dx <
0
){
x += (myBitmap.getWidth()/(coordinateX *
4
));
if
(x > (myBitmap.getWidth() - bmpWidth)){
x = myBitmap.getWidth() - bmpWidth;
}
}
}
if
(coordinateY >
1
){
if
(dy >
0
){
y -= (myBitmap.getHeight()/(coordinateY *
4
));
if
(y <
0
){
y =
0
;
}
}
if
(dy <
0
){
y += (myBitmap.getHeight()/(coordinateY *
4
));
if
(y > (myBitmap.getHeight() - bmpHight)){
y = myBitmap.getHeight() - bmpHight;
}
}
}
bitmap = Bitmap.createBitmap(bitmap, x, y, bmpWidth, bmpHight);
bitmap = MyBitMap.createScaledBitmap(bitmap, dstWidth, dstHeight, ScaleType.FIT_XY);
return
bitmap;
}
private
OnTouchListener ImageOpenListener =
new
OnTouchListener() {
int
lastX;
int
lastY;
int
left;
int
right;
int
top;
int
bottom;
@Override
public
boolean
onTouch(View v, MotionEvent event) {
switch
(event.getAction()){
case
MotionEvent.ACTION_DOWN:
lastX = (
int
)event.getRawX();
lastY = (
int
)event.getRawY();
break
;
case
MotionEvent.ACTION_MOVE:
int
dx = (
int
)event.getRawX() - lastX;
int
dy = (
int
)event.getRawY() - lastY;
Bitmap newBitmap;
if
(bmpSizeWidth > screenWidth||bmpSizeHight > screenHeight){
if
(bmpSizeWidth > screenWidth){
left =
0
;
right = screenWidth;
}
else
{
left = v.getLeft() + dx;
right = v.getRight() + dx;
}
if
(bmpSizeHight > screenHeight){
top =
0
;
bottom = screenHeight;
}
else
{
top = v.getTop() + dy;
bottom = v.getBottom() + dy;
}
if
((dx >
3
|| dx < -
3
) && (dy >
3
||dy < -
3
)){
newBitmap = movCal(myBitmap, dx, dy);
myImageView.setImageBitmap(newBitmap);
}
}
else
{
left = v.getLeft() + dx;
top = v.getTop() + dy;
bottom = v.getBottom() + dy;
right = v.getRight() + dx;
}
v.layout(left, top, right, bottom);
lastX = (
int
)event.getRawX();
lastY = (
int
)event.getRawY();
break
;
}
return
true
;
}
};
@Override
protected
void
onPause() {
System.exit(
0
);
super
.onPause();
}
@Override
protected
void
onStop() {
System.exit(
0
);
super
.onStop();
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}