赞
踩
<RelativeLayout 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"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/icon" />
<Button
android:id="@+id/scale_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<EditText
android:id="@+id/scale_editView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:onClick="direct_scale"
android:text="direct_scale" />
</RelativeLayout>
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 加载Imageview和获得图片的信息
final ImageView imageView = (ImageView) findViewById(R.id.imgView);
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
final Button scale_btn = (Button) findViewById(R.id.scale_btn);
final EditText scale_text = (EditText) findViewById(R.id.scale_editView);
scale_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String scaleStr = scale_text.getText().toString();
if (scaleStr == null || "".equals(scaleStr))
return;
float scale = 0.0f;
try {
scale = Float.parseFloat(scaleStr);
} catch (NumberFormatException e) {
return;
}
imageView.setImageBitmap(scaleBitmap(bitmap,scale));
}
});
}
public void direct_scale(View v)
{
}
private Bitmap scaleBitmap(Bitmap bitmap ,float scale)
{
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
return Bitmap.createBitmap(bitmap, 0, 0,
bitmapWidth, bitmapHeight, matrix, true);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。