Android控件详解之ImageView

来源:互联网 发布:图像识别算法matlab 编辑:程序博客网 时间:2024/06/08 07:42

我将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;@Overridepublic 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;}@Overridepublic 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();@Overridepublic 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 + "度");} }@Overridepublic void onStartTrackingTouch(SeekBar seekBar){// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar){// TODO Auto-generated method stub}@Overridepublic 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 0
原创粉丝点击