《老罗Androi》学习之UI ImageView、SeekBar

1. SeekBar
  SeekBar控件可以通过拖动滑竿改变当前的值,可以利用SeekBar来设置具有一定范围的变量的值。
1. 布局
<SeekBar android:id="@+id/seekbar2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:max="100"
android:progress="30" android:secondaryProgress="60"></SeekBar>
max最大刻度是100,progress当前刻度30,secondaryProgress第二刻度60.
SeekBar可用于音乐,视频播放器等,例如视频progress是当前播放刻度,secondaryProgress是缓冲刻度。
2. 代码
public class Main extends Activity implements OnSeekBarChangeListener {
	private TextView textView1, textView2;
	public void onCreate(Bundle savedInstanceState) {
		textView1 = (TextView) this.findViewById(R.id.textview1);
		textView2 = (TextView) this.findViewById(R.id.textview2);
		SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1);
		SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
		seekBar1.setOnSeekBarChangeListener(this);
		seekBar2.setOnSeekBarChangeListener(this);
	}
	// 当滑动滑竿的时候会触发的事件
	public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
		if (seekBar.getId() == R.id.seekbar1) {
			textView1.setText("seekbar1的当前位置是:" + progress);
		} else {
			textView2.setText("seekbar2的当前位置是:" + progress);
		}
	}
	// 表示从哪里开始拖动
	public void onStartTrackingTouch(SeekBar seekBar) {
		if (seekBar.getId() == R.id.seekbar1) {
			textView1.setText("seekbar1开始拖动");
		} else {
			textView1.setText("seekbar2开始拖动");
		}
	}
	// 表示从哪里结束拖动
	public void onStopTrackingTouch(SeekBar seekBar) {
		if (seekBar.getId() == R.id.seekbar1) {
			textView1.setText("seekbar1停止拖动");
		} else {
			textView1.setText("seekbar2停止拖动");
		}
	}
}
2. ImageView
ImageView主要是用来显示图片的控件,可以对图片进行放大、缩小和旋转的功能。
例1:基本用法 
1.布局
<ImageView android:id="@+id/imageview2" android:layout_width="300dp"
android:layout_height="200dp" android:background="#FFF"
android:src="@drawable/background" android:scaleType="fitCenter"
android:padding="10dp"></ImageView>
android:scaleType属性指定ImageView控件显示图片的方式,例如:center表示图像以不缩放的方式显示在ImageView控件的中心,如果设置为fitCenter,表示图像按照比例缩放至合适的位置,并在ImageView控件的中心
2.代码
// 设置第一个图片的比例大小, 表示宽度:200    高度是100
imageView.setLayoutParams(new LinearLayout.LayoutParams(200, 100));
setTitle("height:" + imageView.getLayoutParams().height + "--width-->>"
	+ imageView.getLayoutParams().width);//设置Activity的TiTle.

如果没有fitCenter就是裁剪,有的话就是缩放。

 注意:---> 使用ImageView应该注意的地方

例2:ImageView实现图片裁剪和显示的功能
1. 布局
两个Button,一个ImageView显示图片。
2. Activity
public class Main extends Activity implements OnClickListener {
	private Button selectImageBtn;
	private Button cutImageBtn;
	private ImageView imageView;
	// 声明两个静态的整型变量,主要是用于意图的返回的标志
	private static final int IMAGE_SELECT = 1;// 选择图片
	private static final int IMAGE_CUT = 2;// 裁剪图片
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		selectImageBtn = (Button) this.findViewById(R.id.selectImageBtn);
		cutImageBtn = (Button) this.findViewById(R.id.cutImageBtn);
		imageView = (ImageView) this.findViewById(R.id.imageview);
		selectImageBtn.setOnClickListener(this);
		cutImageBtn.setOnClickListener(this);
		// imageView.setImageBitmap(bm);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			// 处理图片按照手机的屏幕大小显示
	          if (requestCode == IMAGE_SELECT) {
	          	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 bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null,factory);
	          		// 对图片的宽度和高度对应手机的屏幕进行匹配
	          		int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);
	          		// 如果大于1 表示图片的高度大于手机屏幕的高度
	          		int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);
	          		// 如果大于1 表示图片的宽度大于手机屏幕的宽度
	          		// 缩放到1/radio的尺寸和1/radio^2像素
	          		if (hRatio > 1 || wRatio > 1) {
	          			if (hRatio > wRatio) {
	          				factory.inSampleSize = hRatio;
	          			} else {
	          				factory.inSampleSize = wRatio;
	          			}
	          		}
	          		//查询,而不分配内存空间
	          		factory.inJustDecodeBounds = false;
	          		// 使用BitmapFactory对图片进行适屏的操作,
	          		bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, factory);
	          		imageView.setImageBitmap(bmp);
	          	} catch (Exception e) {
	          	}
	          	// 表示裁剪图片
			} else if (requestCode == IMAGE_CUT) {
				Bitmap bitmap = data.getParcelableExtra("data");
				imageView.setImageBitmap(bitmap);
			}
		}
	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.selectImageBtn:
			// 如何提取手机的图片库,并且进行选择图片的功能
	           	Intent intent = new Intent(Intent.ACTION_PICK,
	           	android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 打开手机的图片库
	           	startActivityForResult(intent, IMAGE_SELECT);//要求返回结果
	           	break;
		case R.id.cutImageBtn:
			Intent intent2 = getImageClipIntent();//用一个方法来生成
			startActivityForResult(intent2, IMAGE_CUT);
			break;
		}
	}
	private Intent getImageClipIntent() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		// 实现对图片的裁剪,必须要设置图片的属性和大小
		intent.setType("image/*");// 获取任意的图片类型
		intent.putExtra("crop", "true");// 滑动选中图片区域
		intent.putExtra("aspectX", 1);// 表示剪切框的比例1:1的效果
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 80);// 指定输出图片的大小
		intent.putExtra("outputY", 80);
		intent.putExtra("return-data", true);
		return intent;
	}
}
例3:ImageView实现图片旋转功能
1.布局
  要是想实现图像的旋转可以使用android.graphics.Matirx类的setRotate来实现。
2. Activity
public class Main extends Activity implements OnSeekBarChangeListener{
	private int minWidth=80;
	private TextView textview1,textview2;
	private ImageView imageView;
	private Matrix matrix=new Matrix();	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		textview1=(TextView)this.findViewById();
		.....
		seekBar1.setOnSeekBarChangeListener(this);
		seekBar2.setOnSeekBarChangeListener(this);
		DisplayMetrics dm=new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		seekBar1.setMax(dm.widthPixels-minWidth);
	}	
	// 当滑动滑竿的时候会触发的事件
	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 {
		Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.dog))).getmap();
		matrix.setRotate(progress);//设置翻转的角度
		bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWigth(),bitmap.getHeigth(),matrix,true);
		imageView.setImageBitmap(bitmap);
		textView2.setText(progress+"度");
	}
}
例4:从网络上获取ImageView图像显示在本地
1.添加网络的受权
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
2. Activity
下载一个图片的两种方式InputStream和byte[ ]
<pre name="code" class="java">button.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {
		// try {
		// InputStream inputStream = HttpUtils.getImageViewInputStream();
		// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
		// imageView.setImageBitmap(bitmap);
		// } catch (IOException e) {
		// }
		byte[] data = HttpUtils.getImageViewArray();
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
		imageView.setImageBitmap(bitmap);

	}
});
 3. 下载的工具类 
public class HttpUtils {
	private final static String URL_PATH = "http://192.168.0.102:8080/myhttp/koala.jpg";// 访问网路图片的路径
	public HttpUtils() {
	}
        //从网络中获取图片信息,以流的形式返回
	public static InputStream getImageViewInputStream() throws IOException {
		InputStream inputStream = null;
		URL url = new URL(URL_PATH);
		if (url != null) {
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setConnectTimeout(3000);// 设置连接超时的时间
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setDoInput(true);
			int response_code = httpURLConnection.getResponseCode();
			if (response_code == 200) {
				inputStream = httpURLConnection.getInputStream();
			}
		}
		return inputStream;
	}
	//从网络中获取图片信息,以字节数组的形式返回
	public static byte[] getImageViewArray() {
		byte[] data = null;
		InputStream inputStream = null;
		// 不需要关闭的输出流,直接写入到内存中
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			URL url = new URL(URL_PATH);
			if (url != null) {
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			httpURLConnection.setConnectTimeout(3000);// 设置连接超时的时间
			httpURLConnection.setRequestMethod("GET");// 请求方法
			httpURLConnection.setDoInput(true);// 打开输入流
			int response_code = httpURLConnection.getResponseCode();
			int len = 0;
			byte[] b_data = new byte[1024];

			if (response_code == 200) {
				inputStream = httpURLConnection.getInputStream();
				while ((len = inputStream.read(b_data)) != -1) {
					outputStream.write(b_data, 0, len);
				}
				data = outputStream.toByteArray();
			}
			}
		} catch (Exception e) {
		} finally {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			}
		}
		return data;
	}
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值