Android UI控件五
来源:互联网 发布:python 继承 编辑:程序博客网 时间:2024/06/05 20:09
Android UI控件五
一、ImageView显示图像控件
ImageView主要是用来显示图片的控件,可以对图片进行放大、缩小和旋转的功能。
Android:scaleType属性指定ImageView控件显示图片的方式,例如:center表示图像以不缩放的方式显示在ImageView控件的中心,如果设置为fitCenter,表示图像按照比例缩放至合适的位置,并在ImageView控件的中心。
1.ImageView控件的基本用法
public class MainActivity extends Activity {private ImageView imageView;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);imageView = (ImageView) findViewById(R.id.imageview01);// 设置第一张图片的比例大小imageView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));setTitle("height=" + imageView.getLayoutParams().height + ">>width="+ imageView.getLayoutParams().width);}}
<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" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="scaleType:center为不缩放显示在ImageView控件中心" /> <ImageView android:id="@+id/imageview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:src="@drawable/bg" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="scaleType:fitcenter按比例缩放显示在ImageView控件中心" /> <ImageView android:id="@+id/imageview01" android:layout_width="200dp" android:layout_height="200dp" android:background="#000" android:padding="20dp" android:scaleType="fitCenter" android:src="@drawable/bg" /></LinearLayout>
2.ImageView实现试屏和裁剪图片的功能
public class MainActivity extends Activity implements OnClickListener {private Button selectImageBtn, cutImageBtn;private ImageView imageView;private static final int SELECT_CODE = 1;private static final int CUT_CODE = 2;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);selectImageBtn = (Button) findViewById(R.id.selectbutton);cutImageBtn = (Button) findViewById(R.id.cutbutton);imageView = (ImageView) findViewById(R.id.imageview);imageView.setBackgroundColor(Color.BLACK);selectImageBtn.setOnClickListener(this);cutImageBtn.setOnClickListener(this);}protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {// 处理图片按照手机的屏幕大小实现if (requestCode == SELECT_CODE) {Uri uri = data.getData();// 获得图片的路径int dw = getWindowManager().getDefaultDisplay().getWidth();int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;try {// 实现对图片裁剪的类,是一个匿名内部类BitmapFactory.Options factory = new BitmapFactory.Options();factory.inJustDecodeBounds = true;// 如果设置为true,允许查询图片不是按照像素分配给内存Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null,factory);// 对图片宽高进行匹配int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);// 如果结果大于1,说明图片的高度大于手机屏幕的高度int wRatio = (int) Math.ceil(factory.outHeight / (float) dw);// 如果结果大于1,说明图片的宽度大于手机屏幕的宽度// 缩放到1/radio的尺寸和 radia^2像素if (hRatio > 1 || wRatio > 1) {if (hRatio > wRatio) {factory.inSampleSize = hRatio;} else {factory.inSampleSize = wRatio;}}factory.inJustDecodeBounds = false;// 实用化BitmapFactory对图片进行试屏的操作bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, factory);imageView.setImageBitmap(bitmap);} catch (Exception e) {e.printStackTrace();}} else if (requestCode == CUT_CODE) {Bitmap bitmap = data.getParcelableExtra("data");imageView.setImageBitmap(bitmap);}}}public void onClick(View v) {switch (v.getId()) {case R.id.selectbutton:// 如何提取手机的图片,并且进行选择图片的功能Intent intent1 = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 打开手机图片库startActivityForResult(intent1, SELECT_CODE);break;case R.id.cutbutton:Intent intent2 = getIamgeClipIntent();startActivityForResult(intent2, CUT_CODE);break;}}private Intent getIamgeClipIntent() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);// 实现对图片的裁剪,必须设置图片的属性和大小intent.setType("image/*");// 获取任意的图片类型intent.putExtra("crop", "true");// 滑动选中图片intent.putExtra("aspectX", 1);// 剪切框的比例intent.putExtra("aspectY", 1);// 剪切框的比例intent.putExtra("outputX", 80);// 指定输出图片的大小intent.putExtra("outputY", 80);intent.putExtra("return-data", true);return intent;}}
<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" > <Button android:id="@+id/selectbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SelectImage" /> <Button android:id="@+id/cutbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="CutImage" /> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
实现图像的缩放:改变ImagView控件的大小,将<ImageView>标签的android:scaleType的属性设置为fitCenter,要想实现图像的旋转可以使用android.graphics.Matirx类的setRotate来实现。
public class MainActivity extends Activity implements OnSeekBarChangeListener {private int minWidth = 80;private TextView tv1, tv2;private ImageView imageView;private SeekBar seekBar1, seekBar2;private Matrix matrix = new Matrix();protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv1 = (TextView) findViewById(R.id.textview01);tv2 = (TextView) findViewById(R.id.textview02);imageView = (ImageView) findViewById(R.id.imageview01);seekBar1 = (SeekBar) findViewById(R.id.seekbar01);seekBar2 = (SeekBar) findViewById(R.id.seekbar02);seekBar1.setOnSeekBarChangeListener(this);seekBar2.setOnSeekBarChangeListener(this);DisplayMetrics displayMetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);seekBar1.setMax(displayMetrics.widthPixels - minWidth);}public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {if (seekBar.getId() == R.id.seekbar01) {int width = minWidth + progress;int height = (int) (width * 3 / 4.0);imageView.setLayoutParams(new LinearLayout.LayoutParams(width,height));tv1.setText("宽度:" + width + "dp,高度:" + height + "dp");} else if (seekBar.getId() == R.id.seekbar02) {Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.bg)).getBitmap();matrix.setRotate(progress);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);imageView.setImageBitmap(bitmap);tv2.setText("旋转" + progress + "度");}}public void onStartTrackingTouch(SeekBar seekBar) {}public void onStopTrackingTouch(SeekBar seekBar) {}}
<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" > <ImageView android:id="@+id/imageview01" android:layout_width="200dp" android:layout_height="150dp" android:scaleType="fitCenter" android:src="@drawable/bg" /> <TextView android:id="@+id/textview01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="宽度:200dp,高度:150dp" /> <SeekBar android:id="@+id/seekbar01" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:max="240" android:progress="120" /> <TextView android:id="@+id/textview02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="旋转0度" /> <SeekBar android:id="@+id/seekbar02" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:max="360" android:progress="0" /></LinearLayout>
4.ImageView从网络上获取图像
public class MainActivity extends Activity {private Button btn;private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button) findViewById(R.id.button);imageView = (ImageView) findViewById(R.id.imageview);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {/*InputStream inputStream = HttpUtils.getImageInputStream();Bitmap bitmap = BitmapFactory.decodeStream(inputStream);imageView.setImageBitmap(bitmap);*/byte[] data = HttpUtils.getImageArray();Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);imageView.setImageBitmap(bitmap);} catch(Exception e) {e.printStackTrace();}}});}}
HttpUtils类
public class HttpUtils {//访问的图片地址private static final String path = "http://192.168.137.98:8080/web/101.jpg";/** * 以字节流的形式返回 * * @return * @throws Exception */public static InputStream getImageInputStream() throws Exception {InputStream inputStream = null;URL url = new URL(path);if (url != null) {HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");conn.setDoInput(true);if (conn.getResponseCode() == 200) {inputStream = conn.getInputStream();}}return inputStream;}/** * 以字节数组的形式返回 */public static byte[] getImageArray() throws Exception {byte[] data = null;InputStream inputStream = null;ByteArrayOutputStream outputStream = new ByteArrayOutputStream();URL url = new URL(path);if (url != null) {HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");conn.setDoInput(true);byte[] buffer = new byte[1024];int len = 0;if (conn.getResponseCode() == 200) {inputStream = conn.getInputStream();while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}data = outputStream.toByteArray();}inputStream.close();}return data;}}
<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" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从网络上获取图片" /> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
- Android UI控件五
- Android 基本UI(五)—图片控件ImageView
- android UI小结(五)
- android UI控件学习
- Android UI控件一
- Android UI控件二
- Android UI控件三
- Android UI控件四
- Android UI控件再试
- Android ui控件详解
- Android控件:高仿微信主UI
- Android UI控件大全
- Android UI控件
- Android之UI控件
- Android中的UI控件
- Android-基本UI控件
- Android UI控件
- android UI基本控件
- 模拟数字显示管
- 关于Spring中的property editor及converter的一些资料
- vb调用webservice,但是通过代理却不行
- 代码定位:__FILE__, __FUNCTION__, __LINE__
- svn返回值(403 Forbidden)的问题
- Android UI控件五
- C++基本功和 Design Pattern系列(1-3) - Inheritance VS Delegation
- _declspec(dllexport)修饰符的用法
- Struts2的配置文件详解
- 获取手机中已安装apk文件信息
- JS中命名空间(namespace)的概念和使用
- 程序员职业规划文字摘录二
- java 虚拟机内存设置
- Oracle通用分页存储过程的创建与使用