Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Andriod SeekBar以及實現圖片旋轉平移放大縮小

Andriod SeekBar以及實現圖片旋轉平移放大縮小

編輯:關於Android編程

首先通過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();
}
}
}
}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved