关闭

利用ImageView和SeekBar动态缩放和旋转图片的Demo

标签: AndroidDisplayMetricsMatrix
439人阅读 评论(0) 收藏 举报
分类:
布局代码:<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"
    tools:context="${relativePackage}.${activityClass}" >


    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/android_l" />


    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />


    <SeekBar
        android:id="@+id/seekbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="旋转度数:0" />


    <SeekBar
        android:id="@+id/seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:max="360"/>


</LinearLayout>


结合屏幕与SeekBar获取缩放:DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
mSeekBar1.setMax(mDisplayMetrics.widthPixels - MIN_WIDTH);
改变seekbar的图片响应代码:if (seekBar.getId() == R.id.seekbar1)
{
int newwidth = progress + MIN_WIDTH;
int newheight = (int) (progress * 3 / 4);
mImageView.setLayoutParams(new LinearLayout.LayoutParams(newwidth, newheight));
mTextView1.setText(getResources().getString(R.string.image_width) + progress + " ");
} else if (seekBar.getId() == R.id.seekbar2)
{
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.android_l)).getBitmap();
Matrix matrix = new Matrix();
matrix.setRotate(progress);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
mImageView.setImageBitmap(bitmap);
mTextView2.setText(getResources().getString(R.string.rotate) + progress);
}

总代码:package com.example.imageview;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends Activity implements OnSeekBarChangeListener
{
private ImageView mImageView;
private TextView mTextView1, mTextView2;
private SeekBar mSeekBar1, mSeekBar2;
private final static int MIN_WIDTH = 200;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setControl();
setScreenMaxSeekBAr1();
SetonChangSeekBar();
}


public void setControl()
{
mImageView = (ImageView) findViewById(R.id.image_view);
mSeekBar1 = (SeekBar) findViewById(R.id.seekbar1);
mSeekBar2 = (SeekBar) findViewById(R.id.seekbar2);
mTextView1 = (TextView) findViewById(R.id.textview1);
mTextView2 = (TextView) findViewById(R.id.textview2);
}


public void SetonChangSeekBar()
{
mSeekBar1.setOnSeekBarChangeListener(this);
mSeekBar2.setOnSeekBarChangeListener(this);
}


private void setScreenMaxSeekBAr1()
{
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
mSeekBar1.setMax(mDisplayMetrics.widthPixels - MIN_WIDTH);
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (seekBar.getId() == R.id.seekbar1)
{
int newwidth = progress + MIN_WIDTH;
int newheight = (int) (progress * 3 / 4);
mImageView.setLayoutParams(new LinearLayout.LayoutParams(newwidth, newheight));
mTextView1.setText(getResources().getString(R.string.image_width) + progress + " ");
} else if (seekBar.getId() == R.id.seekbar2)
{
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.android_l)).getBitmap();
Matrix matrix = new Matrix();
matrix.setRotate(progress);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
mImageView.setImageBitmap(bitmap);
mTextView2.setText(getResources().getString(R.string.rotate) + progress);
}


}


@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub


}


@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub


}


}只是自己的小程序。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10254次
    • 积分:560
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:42篇
    • 译文:2篇
    • 评论:7条