android imageview布局问题
来源:互联网 发布:汉罗塔c语言 编辑:程序博客网 时间:2024/06/04 20:12
第一种情况:图片属性设置为红色字体部分的图如下
布局文件如下
<?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"
android:id="@+id/line"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个" />
</LinearLayout>
第二种情况,布局界面图如下
三张分别为同一布局文件,但图片位置大小不一样
布局文件
<?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"
android:id="@+id/line"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个" />
</LinearLayout>
第三中固定高度的布局效果图
001
package
com.bitmap;
002
003
004
import
android.app.Activity;
005
import
android.graphics.Bitmap;
006
import
android.graphics.BitmapFactory;
007
import
android.graphics.Color;
008
import
android.graphics.drawable.BitmapDrawable;
009
import
android.os.Bundle;
010
import
android.view.View;
011
import
android.view.View.OnClickListener;
012
import
android.widget.Button;
013
import
android.widget.ImageView;
014
import
android.widget.LinearLayout;
015
016
public
class
BitmapTest
extends
Activity {
017
private
LinearLayout line;
018
private
ImageView imageView;
019
private
Button button1;
020
Bitmap bmp1,bmp2,bmp3,bmp4,bmp5;
021
private
int
i=
0
;
022
@Override
023
public
void
onCreate(Bundle savedInstanceState) {
024
super
.onCreate(savedInstanceState);
025
setContentView(R.layout.main);
026
button1=(Button)findViewById(R.id.button1);
027
line=(LinearLayout)findViewById(R.id.line);
028
imageView=(ImageView)findViewById(R.id.imageView);
029
BitmapFactory.Options opts =
new
BitmapFactory.Options();
030
opts.inJustDecodeBounds =
true
;
031
BitmapFactory.decodeFile(
"/sdcard/picturetest/a1.jpg"
, opts);
032
033
opts.inSampleSize = computeSampleSize(opts, -
1
,
480
*
800
);
034
opts.inJustDecodeBounds =
false
;
035
try
{
036
bmp1 = BitmapFactory.decodeFile(
"/sdcard/picturetest/a1.jpg"
, opts);
037
bmp2 = BitmapFactory.decodeFile(
"/sdcard/picturetest/a2.jpg"
, opts);
038
bmp3 = BitmapFactory.decodeFile(
"/sdcard/picturetest/a3.jpg"
, opts);
039
bmp4 = BitmapFactory.decodeFile(
"/sdcard/picturetest/a4.jpg"
, opts);
040
bmp5 = BitmapFactory.decodeFile(
"/sdcard/picturetest/a5.jpg"
, opts);
041
imageView.setImageBitmap(bmp1);
042
}
catch
(OutOfMemoryError err) {
043
}
044
045
046
button1.setOnClickListener(
new
OnClickListener(){
047
048
@Override
049
public
void
onClick(View arg0) {
050
// TODO Auto-generated method stub
051
if
(i==
0
){
052
imageView.setImageBitmap(bmp2);
053
if
(!bmp1.isRecycled() ){
054
bmp1.recycle() ;
//回收图片所占的内存
055
java.lang.System.gc();
//提醒系统及时回收
056
}
057
}
058
059
if
(i==
1
){
060
imageView.setImageBitmap(bmp3);
061
062
}
063
if
(i==
2
){
064
imageView.setImageBitmap(bmp4);
065
if
(!bmp3.isRecycled() ){
066
bmp3.recycle() ;
//回收图片所占的内存
067
java.lang.System.gc();
//提醒系统及时回收
068
}
069
}
070
if
(i==
3
){
071
imageView.setImageBitmap(bmp5);
072
if
(!bmp4.isRecycled() ){
073
bmp4.recycle() ;
//回收图片所占的内存
074
// java.lang.System.gc(); //提醒系统及时回收
075
}
076
}
077
i++;
078
079
}
080
081
082
083
084
});
085
086
087
088
089
}
090
091
092
093
public
static
int
computeSampleSize(BitmapFactory.Options options,
094
int
minSideLength,
int
maxNumOfPixels) {
095
int
initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
096
097
int
roundedSize;
098
if
(initialSize <=
8
) {
099
roundedSize =
1
;
100
while
(roundedSize < initialSize) {
101
roundedSize <<=
1
;
102
}
103
}
else
{
104
roundedSize = (initialSize +
7
) /
8
*
8
;
105
}
106
107
return
roundedSize;
108
}
109
110
private
static
int
computeInitialSampleSize(BitmapFactory.Options options,
int
minSideLength,
int
maxNumOfPixels) {
111
double
w = options.outWidth;
112
double
h = options.outHeight;
113
114
int
lowerBound = (maxNumOfPixels == -
1
) ?
1
:
115
(
int
) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
116
int
upperBound = (minSideLength == -
1
) ?
128
:
117
(
int
) Math.min(Math.floor(w / minSideLength),
118
Math.floor(h / minSideLength));
119
if
(upperBound < lowerBound) {
120
return
lowerBound;
121
}
122
123
if
((maxNumOfPixels == -
1
) &&
124
(minSideLength == -
1
)) {
125
return
1
;
126
}
else
if
(minSideLength == -
1
) {
127
return
lowerBound;
128
}
else
{
129
return
upperBound;
130
}
131
132
133
}
134
135
136
}
- android imageview布局问题
- android imageview布局问题
- android 相对布局,代码创建imageview,布局居中问题
- Android 不规则多边形ImageView布局问题,大神在哪里?
- Android 实现不规则ImageView布局
- Android布局相关&&ImageView.scaleType
- Android引入布局 GridView实现ImageView+TextView
- Android布局优化之TextView、ImageView合二为一
- android imageView 图片自适应问题
- android ImageView遇到的问题
- 对于图片的处理问题ImageView的填充布局
- 如何将Android ImageView放到布局的中心
- 如何将Android ImageView放到布局的中心
- android 在xml布局文件中 ImageView ImageButton出现警告
- android ImageView布局时出现警告的解决
- android ImageView布局时出现警告的解决
- Android ImageView,布局或控件显示网络图片教程
- android imageview设置图片的小问题
- SQL Server中的加密
- 如何修复google自动跳转google.hk?
- Android 仿QQ主页面的实
- 用epoll实现毫秒sleep
- Android ListView使用ArrayAdapter
- android imageview布局问题
- MSVC與CRT的恩怨情仇
- NYOJ 56 阶乘因式分解(一)
- JavaScript中Element与Node的区别,children与childNodes的区别
- matlab-runtime error问题
- The path "" is not a valid path to the 3.5.0-44-generic kernel headers.
- (转)jni jstring 、char* 类型的转换
- C# 生成缩略图
- SQL Server中的权限体系(上)-主体