package com.example.androidmatrix;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
ImageView imageSource, imageTarget;
Bitmap bitmapSource;
SeekBar rotateBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSource = (ImageView)findViewById(R.id.source);
imageTarget = (ImageView)findViewById(R.id.target);
rotateBar = (SeekBar)findViewById(R.id.rotatebar);
bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
imageSource.setImageBitmap(bitmapSource);
rotateBar.setOnSeekBarChangeListener(rotateBarChangeListener);
Bitmap nBM = rotateBitmap(bitmapSource, 0.0f);
imageTarget.setImageBitmap(nBM);
}
OnSeekBarChangeListener rotateBarChangeListener
= new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
float rotateDegrees = (float)(rotateBar.getProgress());
imageTarget.setImageBitmap(rotateBitmap(bitmapSource, rotateDegrees));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}};
private Bitmap rotateBitmap(Bitmap src, float degrees){
Matrix matrix = new Matrix();
matrix.postRotate(degrees, src.getWidth()/2, src.getHeight()/2);
Bitmap rotatedBitmap = Bitmap.createBitmap(
bitmapSource,
0,
0,
src.getWidth(),
src.getHeight(),
matrix,
true);
return rotatedBitmap;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<ImageView
android:id="@+id/source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/rotatebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="360"
android:progress="0"/>
</LinearLayout>
Download the files.
No comments:
Post a Comment