自带工具栏(导航栏)的imageview

目标:在imageview的顶部设计一个工具栏,具有放大,缩小,裁剪的功能。

public class MainActivity extends Activity {
String TAG="MainActivity";

ImageView mImageView;

ShowPicture mShowPicture;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            
        Button OpenPicture= (Button) findViewById(R.id.OpenPictureButton);
        OpenPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
 //               intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。
                intent.setType("image/*");
                //intent.setType(“audio/*”); //选择音频
                //intent.setType(“video/*”); //选择视频 (mp4 3gp 是android支持的视频格式)
                //intent.setType(“video/*;image/*”);//同时选择视频和图片
                 


                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,1);
    //            startActivityForResult(Intent.createChooser(intent, "请选择文件!"),1);
            }
        });    
        
       
         mImageView=(ImageView) findViewById(R.id.imageView1);
          
         
    }

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择,没选择就不会继续
            Uri uri = data.getData();//得到uri,后面就是将uri转化成file的过程。
            
  //          Log.i(TAG,"Path"+uri.getPath());
            
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            String img_path = actualimagecursor.getString(actual_image_column_index);
            File file = new File(img_path);
            String FileName=file.toString();
            Toast.makeText(MainActivity.this, FileName, Toast.LENGTH_SHORT).show();
            Log.i(TAG,FileName);
            
            mShowPicture=new ShowPicture(MainActivity.this,mImageView,FileName);
  
  //   Bitmap bitmap = BitmapFactory.decodeFile(FileName);
  //         mImageView.setImageBitmap(bitmap);
            
            
        }
    }  
    

    }

//////////////////自带工具栏(导航栏)的imageview//////////

public class ShowPicture {
Bitmap bitmap;
Bitmap ShowBitmap,ScaledBitmap,FinalBitmap;
ImageView mImageView;
ZoomControls mZoomContorl;
int ImageWidth;
int ImageHeight;
    int ImageViewWidth,ImageViewHeight;
    double ScaleWidth,ScaleHeight,Scale=1;
    int mScaleMinus=1,mScalePlus=1;
    String TAG="ShowPicture";
    boolean IsTouchDown=false,IsCrop=false;
    int FirstX,FirstY,SecondX,SecondY;


    int FontSize=30;
    int ToolBarIntevalPixel=100;
    Paint paintAll;
    boolean IsDrawCropRect=false; 
    Activity mMainActivity;
    
    
public ShowPicture(Activity MainActivity,ImageView myImageView,String ImagePath) {
// TODO Auto-generated constructor stub
mMainActivity=MainActivity;
       mImageView=myImageView;
    
       
       
    ImageViewWidth=myImageView.getWidth();
    ImageViewHeight=myImageView.getHeight();
    FinalBitmap=Bitmap.createBitmap(ImageViewWidth, ImageViewHeight, Bitmap.Config.ARGB_8888);
    CreatePaint();
   
  bitmap = BitmapFactory.decodeFile(ImagePath);
   
    ImageWidth=bitmap.getWidth();
    ImageHeight=bitmap.getHeight();
 
    ScaleWidth=ImageViewWidth/ImageWidth;
    ScaleHeight=ImageViewHeight/ImageHeight;
   
    if(ScaleWidth<1 ||ScaleHeight<1){
    //裁剪图片
    ShowBitmap=CropBitmap(bitmap,ImageWidth,ImageHeight,ImageViewWidth,ImageViewHeight);
    }else{
    ShowBitmap=bitmap;
    }
   
    DrawFinalImage();
 
  mImageView.setOnTouchListener(new ImageView.OnTouchListener() {

@Override public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub 
int touch_X = (int) event.getX(); int touch_Y = (int) event.getY();
int w=v.getWidth(); int h=v.getHeight();
int ShowImageW=ShowBitmap.getWidth(),ShowImageH=ShowBitmap.getHeight();

Log.i("onTouch",touch_X+","+touch_Y);

switch (event.getAction()) {
 
  case KeyEvent.ACTION_UP:
  {
//松开事件发生后执行代码的区域                  
  ActionOn(touch_X,touch_Y);
  
   IsTouchDown=false; 
   break;
  }
  case KeyEvent.ACTION_DOWN:
  {
// 按住事件发生后执行代码的区域                   
FirstX=touch_X;FirstY=touch_Y;
break;
  }
  
  default:
//   Log.i("onTouch","Moving "+event.getAction()+","+touch_X+","+touch_Y);
  if(IsCrop){
SecondX=touch_X;SecondY=touch_Y;

/* Bitmap bmp=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
   Canvas canvas=new Canvas(bmp);

   Paint paintAll=new Paint();
   paintAll.setAlpha(255);//不透明度//完全不透明
   paintAll.setAntiAlias(true);
   paintAll.setColor(Color.rgb(0,0,0));//黑色//默认就是黑色
   paintAll.setStyle(Paint.Style.FILL);
   
   
canvas.drawBitmap(ShowBitmap, (w-ShowImageW)/2, (h-ShowImageH)/2, paintAll);
paintAll.setStyle(Paint.Style.STROKE);
   canvas.drawRect(FirstX, FirstY, SecondX, SecondY, paintAll);
*/
   IsDrawCropRect=true;
   DrawFinalImage();
   IsDrawCropRect=false;


  
  }
  
  
  break;
  }
 


/* IsTouchDown=!IsTouchDown;

if(IsTouchDown){
FirstX=touch_X;FirstY=touch_Y;
}else{

  
}
*/

//
return true;

}
});
 
 
 
 
}

private void DrawFinalImage(){


int ShowImageW=ShowBitmap.getWidth(),ShowImageH=ShowBitmap.getHeight();


FinalBitmap=Bitmap.createBitmap(ImageViewWidth, ImageViewHeight, Bitmap.Config.ARGB_8888);


Canvas canvas=new Canvas(FinalBitmap);
    
canvas.drawBitmap(ShowBitmap, (ImageViewWidth-ShowImageW)/2, (ImageViewHeight-ShowImageH)/2, paintAll);


// Log.i("DrawFinalImage","开始画顶部工具栏");
canvas.drawText("--",paintAll.getStrokeWidth(),FontSize-paintAll.getStrokeWidth(),paintAll);
    canvas.drawCircle(FontSize/2, FontSize/2, FontSize/2, paintAll);


canvas.drawText("+",ToolBarIntevalPixel+paintAll.getStrokeWidth(),FontSize-paintAll.getStrokeWidth(),paintAll);
    canvas.drawCircle(ToolBarIntevalPixel+FontSize/2, FontSize/2, FontSize/2, paintAll);

    canvas.drawRect(ToolBarIntevalPixel*2, 0, ToolBarIntevalPixel*2+FontSize, FontSize, paintAll);
    
    if(IsDrawCropRect){
    canvas.drawRect(FirstX, FirstY, SecondX, SecondY, paintAll);
    }
  
 //****   
    
    
    
mImageView.setImageBitmap(FinalBitmap);
  
    
}



private void CreatePaint(){

    paintAll=new Paint();
    paintAll.setAlpha(255);//不透明度//完全不透明
    paintAll.setAntiAlias(true);
    paintAll.setColor(Color.rgb(0,0,0));//黑色//默认就是黑色
//    paintAll.setStyle(Paint.Style.FILL);
paintAll.setStyle(Paint.Style.STROKE);


paintAll.setStrokeWidth(5);
paintAll.setTextSize(FontSize);
   
}


private void ActionOn(int X,int Y){

if(X<FontSize && Y<FontSize ){
if(FirstX<FontSize && FirstY<FontSize){
ZoomOut();
return;
}
}

if(X<ToolBarIntevalPixel+FontSize && X>ToolBarIntevalPixel && Y<FontSize ){
if(FirstX<ToolBarIntevalPixel+FontSize && FirstX>ToolBarIntevalPixel && FirstY<FontSize){
ZoomIn();
return;
}
}

if(X<ToolBarIntevalPixel*2+FontSize && X>ToolBarIntevalPixel*2 && Y<FontSize ){
if(FirstX<ToolBarIntevalPixel*2+FontSize && FirstX>ToolBarIntevalPixel*2 && FirstY<FontSize){
IsCrop=true;
return;
}
}

if(IsCrop){
        Toast.makeText(mMainActivity, "Crop the picture", Toast.LENGTH_SHORT).show();

}



}


private void ZoomOut(){

    if(mScalePlus==1){

  if(mScaleMinus<=4){
  mScaleMinus*=2;
    }else{
    return;
    }
     
}else if(mScalePlus>=2){
mScalePlus/=2;
mScaleMinus=1;
}else{
mScaleMinus=1;
mScaleMinus=1;
}

Bitmap ScaledBitmap=scaleMyBitmap(bitmap,ImageWidth*mScalePlus/mScaleMinus,ImageHeight*mScalePlus/mScaleMinus);
int CurrentImageWidth=ScaledBitmap.getWidth();
int CurrentImageHeight=ScaledBitmap.getHeight();


ScaleWidth=ImageViewWidth/CurrentImageWidth;
ScaleHeight=ImageViewHeight/CurrentImageHeight;

    if(ScaleWidth<1 ||ScaleHeight<1){
//裁剪图片
          ShowBitmap=CropBitmap(ScaledBitmap,CurrentImageWidth,CurrentImageHeight,ImageViewWidth,ImageViewHeight);
    }else{
ShowBitmap=ScaledBitmap;
}
   
    DrawFinalImage();
 
// mImageView.setImageBitmap(FinalBitmap);

}


private void ZoomIn(){

    //放大    
if(mScaleMinus==1){
if(mScalePlus<=2){
  mScalePlus*=2;
}else{
return;
}

    }else if(mScaleMinus>=2){
    mScaleMinus/=2;
    mScalePlus=1;
    }else{
    mScaleMinus=1;
    mScaleMinus=1;
    }



ScaledBitmap=scaleMyBitmap(bitmap,ImageWidth*mScalePlus/mScaleMinus,ImageHeight*mScalePlus/mScaleMinus);
int CurrentImageWidth=ScaledBitmap.getWidth();
int CurrentImageHeight=ScaledBitmap.getHeight();

ScaleWidth=ImageViewWidth/CurrentImageWidth;
ScaleHeight=ImageViewHeight/CurrentImageHeight;

Log.i(TAG,CurrentImageWidth+"*"+CurrentImageHeight);

    if(ScaleWidth<1 ||ScaleHeight<1){
//裁剪图片
          ShowBitmap=CropBitmap(ScaledBitmap,CurrentImageWidth,CurrentImageHeight,ImageViewWidth,ImageViewHeight);
    }else{
ShowBitmap=ScaledBitmap;
}
   
    DrawFinalImage();
   
// mImageView.setImageBitmap(FinalBitmap);
// mZoomContorl.bringToFront();
// mZoomContorl.show();



}
private Bitmap CropBitmap(Bitmap bitmap,int ImageWidth,int ImageHeight,int ImageViewWidth,int ImageViewHeight){
Bitmap CroppedBitmap = null;
int cropX=0,cropY=0;


Matrix outputMatrix = new Matrix();
    outputMatrix.setScale(1, 1);
    
    int cropWidth=ImageWidth;
if(ImageViewWidth<ImageWidth){
cropX=(ImageWidth-ImageViewWidth)/2;
cropWidth=ImageViewWidth;
}

int cropHeight=ImageHeight;

if(ImageViewHeight<ImageHeight){
cropY=(ImageHeight-ImageViewHeight)/2;
cropHeight=ImageViewHeight;
}
 
     
return Bitmap.createBitmap(bitmap,
              cropX,  cropY,  cropWidth,  cropHeight,
             outputMatrix, false);



}




public  Bitmap scaleMyBitmap(Bitmap bitmap, int nWidth,int nHeight) {


 int width = bitmap.getWidth(); 
 int height = bitmap.getHeight();  


// // Log.i("Harrison", "nWidth="+nWidth+",  nHeight"+nHeight);
 
 float scaleWidth = ((float) nWidth)/width;  
 float scaleHeight = ((float)nHeight)/height; 
// Log.i("scaleMyBitmap",  width+","+ height);
 
 Matrix matrix = new Matrix(); 
 matrix.postScale(scaleWidth, scaleHeight); 
 
 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
 
 return resizedBitmap;
}    
 
}

//////////////////自带工具栏(导航栏)的imageview//////////

效果图:上面有三个功能键。

自带工具栏(导航栏)的imageview