imageview_实现图像的旋转.

1. 布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ImageView android:id="@+id/imageview" android:layout_width="wrap_content"
		android:layout_height="wrap_content"></ImageView>
	
	<TextView android:id="@+id/textview1"
	    android:layout_width="200dp"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="10dp"
	    android:text="图像宽度:240 高度:160"/>
	
	<SeekBar android:id="@+id/seekbar1"
	    android:layout_width="200dp"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="10dp"
	    android:max="240"
	    android:progress="120"/>
	
	<TextView android:id="@+id/textview2"
	    android:layout_width="200dp"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="10dp"
	    android:text="图像宽度:240 高度:160"/>
	
	<SeekBar android:id="@+id/seekbar2"
	    android:layout_width="200dp"
	    android:layout_height="wrap_content"
	    android:layout_marginTop="10dp"
	    android:max="240"
	    android:progress="120"/>
	
</LinearLayout>


2.代码文件。

使用matrix这个类:

在监听函数里面直接改变全局变量。这样就将图片给改变了。

注意强制将资源转换为一个bitmap的做法。

将一个bitmap和matrix进行整合。然后重新生成一个bitmap就好了。

package com.example.marvinedittext2;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener, OnSeekBarChangeListener {
	private ImageView imageView;
	private int minWidth = 80;
	private Matrix matrix = new Matrix();
	TextView textView1;
	TextView textView2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1);
		SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
		imageView = (ImageView) findViewById(R.id.imageview);
		textView1= (TextView) this.findViewById(R.id.textview1);
		textView2= (TextView) this.findViewById(R.id.textview2);
		
		seekBar1.setOnSeekBarChangeListener(this);
		seekBar2.setOnSeekBarChangeListener(this);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		seekBar1.setMax(dm.widthPixels-minWidth);
		
	}

	
	// 因为这里的类是同时在监听者多个那么就需要用组件的id来直接判断。
	@Override
	public void onClick(View v) {
	}


	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if(seekBar.getId()==R.id.seekbar1){
			int newWidth = progress+minWidth;
			int newHeight = (int)(newWidth*3/4);
			imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
			textView1.setText("图像宽度"+newWidth+"图像高度"+newHeight);
		}else if(seekBar.getId()==R.id.seekbar2){
			//这里直接将资源转化为了一个bitmap的类型
			Bitmap bitmap = ((BitmapDrawable)(getResources().getDrawable(R.drawable.background))).getBitmap();
			matrix.setRotate(progress);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
			imageView.setImageBitmap(bitmap);
			textView2.setText(progress+"度");
		}
		
	}


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


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

}


3.over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下代码为何两根手指放在图片上就闪退 请完善 修改 public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // 手指压下屏幕 mode = MODE.DRAG; // 查找被点击的图片 int index = event.getActionIndex(); float x = event.getX(index); float y = event.getY(index); CustomBitmap clickedBitmap = findClickedBitmap(x, y); if (clickedBitmap != null) { // 切换操作对象 _bitmaps.remove(clickedBitmap); _bitmaps.add(clickedBitmap); // 记录ImageView当前的移动位置 currentMatrix.set(clickedBitmap.matrix); clickedBitmap.matrix.set(currentMatrix); clickedBitmap.startPoint.set(x, y); _curCustomBitmap = clickedBitmap; } postInvalidate(); break; case MotionEvent.ACTION_POINTER_DOWN: // 当屏幕上还有触点(手指),再有一个手指压下屏幕 mode = MODE.ZOOM; // 记录位图的旋转角度和缩放倍数 if (_curCustomBitmap == null) { return true; } _curCustomBitmap.oldRotation = rotation(event); _curCustomBitmap.startDis = distance(event); if (_curCustomBitmap.startDis > 10f) { // 获取缩放中心点的坐标 float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); _curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2); // 记录ImageView当前的缩放倍数 currentMatrix.set(_curCustomBitmap.matrix); } break; case MotionEvent.ACTION_MOVE: // 手指在屏幕移动,该事件会不断地触发 if (mode == MODE.DRAG) { // 移动图片 if (_curCustomBitmap == null) { return true; } float dx = event.getX() - _curCustomBitmap.startPoint.x; float dy = event.getY() - _curCustomBitmap.startPoint.y; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postTranslate(dx, dy); } else if (mode == MODE.ZOOM) { // 缩放和旋转图片 if (_curCustomBitmap == null) { return true; } float endDis = distance(event); float rotation = rotation(event) - _curCustomBitmap.oldRotation; if (endDis > 10f) { float scale = endDis / _curCustomBitmap.startDis; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); _curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); } } break; case MotionEvent.ACTION_POINTER_UP: // 有手指离开屏幕,但屏幕还有触点(手指) if (event.getPointerCount() > 1) { mode = MODE.ZOOM; } else { mode = MODE.NONE; } break; } invalidate(); return true; } // 查找被点击的图片 private CustomBitmap findClickedBitmap(float x, float y) { for (CustomBitmap bitmap : _bitmaps) { float[] values = new float[9]; bitmap.matrix.getValues(values); float globalX = values[Matrix.MTRANS_X]; float globalY = values[Matrix.MTRANS_Y]; float width = values[Matrix.MSCALE_X] * bitmap.getBitmap().getWidth(); float height = values[Matrix.MSCALE_Y] * bitmap.getBitmap().getHeight(); RectF rect = new RectF(globalX, globalY, globalX + width, globalY + height); if (rect.contains(x, y)) { return bitmap; } } return null; } // 计算两点之间的距离 private float distance(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } // 计算两点之间的旋转角度 private float rotation(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); double radians = Math.atan2(y2 - y1, x2 - x1); return (float) Math.toDegrees(radians); } }
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值