ImageView

1.用ImageView的scaleType放置图片

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="不缩放" />

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/abc" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fitCenter按照比例缩放" />

    <ImageView
        android:id="@+id/imageview2"
        android:layout_width="100sp"
        android:layout_height="200sp"
        android:scaleType="fitCenter"
        android:layout_gravity="center"
        android:src="@drawable/pic8" />

</LinearLayout>
package com.example.android_imageview01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView textView1;
	private TextView textView2;
	private ImageView imageView1;
	private ImageView imageView2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView1=(TextView) findViewById(R.id.textview1);
		textView2=(TextView) findViewById(R.id.textview2);
		imageView1=(ImageView) findViewById(R.id.imageview1);
		imageView2=(ImageView) findViewById(R.id.imageview2);
		imageView1.setLayoutParams(new LinearLayout.LayoutParams(300,400));
		setTitle("Height"+imageView1.getLayoutParams().height+"Width"+imageView1.getLayoutParams().width);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

3.ImageView实现图片的缩放旋转

<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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="200sp"
        android:layout_height="150sp" 
        android:scaleType="fitCenter"
        android:src="@drawable/abc"
        />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="图片宽度200sp,高度150sp"
        android:layout_marginTop="10sp"
         />

    <SeekBar
        android:id="@+id/seekbar1"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:max="240"
        android:progress="120" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0度" />

    <SeekBar
        android:id="@+id/seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="360"
        android:progress="0" 
        
        />

</LinearLayout>

package com.example.android_imageview02;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSeekBarChangeListener{

	private int minWidth=80;
	private TextView textView1;
	private TextView textView2;
	private ImageView imageView1;
	private SeekBar seekBar1;
	private SeekBar seekBar2;
	private Matrix matrix=new Matrix();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView1=(TextView) findViewById(R.id.textview1);
		textView2=(TextView) findViewById(R.id.textview2);
		imageView1=(ImageView) findViewById(R.id.imageview1);
		seekBar1=(SeekBar) findViewById(R.id.seekbar1);
		seekBar2=(SeekBar) findViewById(R.id.seekbar2);
		seekBar1.setOnSeekBarChangeListener(this);
		seekBar2.setOnSeekBarChangeListener(this);
		DisplayMetrics displayMetrics=new DisplayMetrics();
		//一个结构描述 大小、密度、字体缩放
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		seekBar1.setMax(displayMetrics.widthPixels-minWidth);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		if(seekBar.getId()==R.id.seekbar1){
			int newWidth=progress+minWidth;
			int newHeight=(int)(newWidth*3/4);
			imageView1.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
			textView1.setText("图片的宽度:"+newWidth+"图片的高度:"+newHeight);
		}else if(seekBar.getId()==R.id.seekbar2){
			Bitmap bitmap=((BitmapDrawable)(getResources().getDrawable(R.drawable.abc))).getBitmap();
			matrix.setRotate(progress);//设置翻转角度
			bitmap=bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(),matrix, true);
			imageView1.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
		
	}

}

4.Imageview从网络获取图片

package com.example.httputils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtils {

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}
	public static InputStream getImageInputStreamFromInternet(String url_path){
		InputStream inputStream=null;
		URL url=null;
		try {
			url=new URL(url_path);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setConnectTimeout(3000);
			httpURLConnection.setDoInput(true);
			int code=httpURLConnection.getResponseCode();
			if(code==200){
				inputStream=httpURLConnection.getInputStream();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return inputStream;
	}
}


package com.example.android_imageview03;

import com.example.httputils.*;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

	private String url="http://a.hiphotos.baidu.com/image/w%3D2048/sign=a3376916d60735fa91f049b9aa690eb3/f703738da9773912bb344e6cfa198618367ae2a9.jpg";
	private ImageView imageView1;
	private Button button1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		imageView1=(ImageView) findViewById(R.id.imageview1);
		button1=(Button) findViewById(R.id.button1);
		button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bitmap bm=BitmapFactory.decodeStream(HttpUtils.getImageInputStreamFromInternet(url));
				imageView1.setImageBitmap(bm);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值