Andriod SeekBar以及实现图片旋转平移放大缩小

首先通过ddms把图片导入模拟器

package com.example.testimageview;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnClickListener
{

 private Button bclick;
 private ImageView imageView;
 private SeekBar sBar;
 private Bitmap bmp;
 
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  bclick = (Button) findViewById(R.id.bclick);
  bclick.setOnClickListener(this);
  imageView = (ImageView) findViewById(R.id.image_view);
  sBar = (SeekBar) findViewById(R.id.sb);
  sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
  {
   
   @Override
   public void onStopTrackingTouch(SeekBar seekBar)
   {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar)
   {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser)
   {
    // TODO Auto-generated method stub
    //创建一个与取出来的图片大小一致的位图文件
    Bitmap alterBmp = bmp.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    
    //画布
    Canvas  canvas = new Canvas(alterBmp);
    //画笔
    Paint paint = new Paint();
    paint.setStrokeWidth(5f);
    //矩阵....
    Matrix matrix = new Matrix(); 
    // scale 缩放    rotate  旋转translate 平移
    //matrix.setRotate((float) 0.9*progress);
    //matrix.setScale(0.1f*progress,0.1f*progress);
    matrix.setTranslate(-0.1f*progress,-0.1f*progress);
    canvas.drawBitmap(bmp, matrix, paint);
    imageView.setImageBitmap(alterBmp);
    
   }
  });
  
 }

 @Override
 public void onClick(View v)
 {
  // TODO Auto-generated method stub
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_PICK);
  intent.setType("image/*");
  //startActivity(intent);
     startActivityForResult(intent, 1);
  
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  // TODO Auto-generated method stub
  if(requestCode==1)
  {
   if(resultCode == RESULT_OK)
   {
    try
    {
     Uri uri = data.getData();
     bmp =MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
     imageView.setImageURI(uri);
    } catch (FileNotFoundException e)
    {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IOException e)
    {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }

}

你可能感兴趣的:(Andriod)