Android控件详解之ImageView

我将Android控件的ImageView的学习知识总结一下和大家共享

在Android开发中,图片显示也是我们必须要用到的,ImagView是非常 常用的图片显示控件。

ImagView控件的基本使用方法很简单,在布局文件中使用<EditText>第一既可以了,或者在java代码:ImagView  imagView= (ImagView)findViewById(R.id.imagView1);

1、ImaGeView图片控件

ImageView控件可以用于显示Android系统支持的图像,其支持的图像格式有gif、jpg、png、bmp等。

<?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">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="scaleType:center  未缩放,放在ImageView的中心" />
	<ImageView android:id="@+id/imageview" android:layout_width="wrap_content"
		android:background="#F00" android:layout_height="wrap_content"
		android:src="@drawable/icon" android:scaleType="center" />
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:layout_marginTop="20dp"
		android:text="scaleType:fitCenter  按比例缩放" />
	<ImageView  android:layout_width="300dp"
		android:background="#FFF" android:layout_height="200dp" android:src="@drawable/background"
		android:scaleType="fitCenter" android:padding="10dp" />
</LinearLayout>
在布局中, android:scaleType="center"是指定ImageView显示图像的方式。center表示图像显示在ImagView控件中心,fitCenter表示将图像按比例缩放至合适的大小,并显示在ImageView控件中心。

下面举一个显示指定区域的图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="fill_parent"
        android:layout_height="261dp"
        android:background="#FFF"
        android:scaleType="fitStart"
        android:src="@drawable/dog" />
        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="10dp"
            android:background="#FFF"
            android:scaleType="fitCenter"/>
</LinearLayout>
java实现代码:

public class Main extends Activity implements OnTouchListener
{
	private ImageView imageView1;
	private ImageView imageView2;

	@Override
	public boolean onTouch(View view, MotionEvent event)
	{
		try
		{
			BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1
					.getDrawable();
			float scale = (float)bitmapDrawable.getBitmap().getHeight()/imageView1.getMeasuredHeight();
			int x = (int) (event.getX() * scale);
			int y = (int) (event.getY() * scale);
			int width = (int)(100 * scale);
			int height = (int)(100 * scale);

			imageView2.setImageBitmap(Bitmap.createBitmap(
					bitmapDrawable.getBitmap(), x, y, width, height));
			

		}
		catch (Exception e)
		{
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		imageView1 = (ImageView) findViewById(R.id.imageview1);
		imageView2 = (ImageView) findViewById(R.id.imageview2);
		imageView1.setOnTouchListener(this);
	}
}
图片显示只是一般的方法,如果要放大缩小和旋转:

<?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" android:gravity="center_horizontal">
	<ImageView android:id="@+id/imageView" android:layout_width="200dp"
		android:layout_height="150dp" android:src="@drawable/dog"
		android:scaleType="fitCenter" />
	<TextView android:id="@+id/textview1" android:layout_width="fill_parent"
		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="fill_parent"
		android:layout_height="wrap_content" android:layout_marginTop="10dp"
		android:text="0度" />
	<SeekBar android:id="@+id/seekBar2" android:layout_width="200dp"
		android:layout_height="wrap_content" android:max="360" />
</LinearLayout>
java实现:

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

	@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 = ((BitmapDrawable) getResources().getDrawable(
					R.drawable.dog)).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

	}

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






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值