java设置imageview图片大小_java – 在android中设置imageview
我有一个JSON URL :: http://54.218.73.244:7006/DescriptionSortedRating/
JSON STRUCT ::
"restaurants": [
{
"restaurantID": 4,
"restaurantNAME": "CopperChimney1",
"restaurantIMAGE": "MarkBoulevard1.jpg",
"restaurantDISTANCE": 15,
"restaurantTYPE": "Indian",
"restaurantRATING": 1,
"restaurantPrice": 11,
"restaurantTime": "9am t0 8pm"
},
RestaurantDescPhotos.java
public class RestaurantDescPhotos extends Activity {
// url to make request
private static String url = "http://54.218.73.244:7006/DescriptionSortedRating/";
String restaurant_name, cc_res;
ProgressDialog progressDialog;
JSONObject jsonObject;
JSONArray first_array;
TextView textView;
TextView text;
private SparseArray imagesMap = new SparseArray();
ArrayList> list_of_images = new ArrayList>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_desc_photos);
progressDialog = new ProgressDialog(RestaurantDescPhotos.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(RestaurantDescPhotos.this, "",
"Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String _response = null;
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response = EntityUtils.toString(resEntity);
jsonObject = new JSONObject(_response);
first_array = jsonObject.getJSONArray("restaurants");
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
// TextView timedisplay=(TextView)
// findViewById(R.id.RestaurantTimeID);
for (int i = 0; i < first_array.length(); i++) {
try {
JSONObject detail_obj = first_array.getJSONObject(i);
HashMap map_for_images = new HashMap();
int id = detail_obj.getInt("_id");
String IMAGES = detail_obj.getString("restaurantIMAGE");
map_for_images.put("Starters", IMAGES);
list_of_images.add(map_for_images);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ImageView imageView = (ImageView) findViewById(R.id.DISP_IMG);
}
}
}
RestaurantDescPhotos.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
android:layout_width="match_parent"
android:layout_height="40dp" >
android:layout_width="match_parent"
android:layout_height="60dp" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/DescriptionTitleRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2sp" >
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
android:id="@+id/DISP_IMG"
android:layout_width="167dp"
android:layout_height="167dp"
android:src="@drawable/ic_launcher" />
ImageLoader.java
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map imageViews = Collections
.synchronizedMap(new WeakHashMap());
ExecutorService executorService;
// Handler to display images in UI thread
Handler handler = new Handler();
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.temp_img;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null)
return b;
// Download Images from the Internet
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
// Find the correct scale value. It should be the power of 2.
// Recommended Size 512
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
try {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
handler.post(bd);
} catch (Throwable th) {
th.printStackTrace();
}
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
>我有一个XML的imageview
>如何设置JSONURL的图像视图
>我编写了课程的一部分,但试图知道如何设置
ImageView的
有任何想法吗
解决方法:
您可以使用ImageLoader设置图像…
全局创建ImageLoader的实例,如..
ImageLoader imageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
......
imageLoader=new ImageLoader(ClassName.this);
ImageView imageView = (ImageView) findViewById(R.id.DISP_IMG);
......
imageLoader.DisplayImage(YourImageURLHere,imageView);
标签:android,java,json,xml,android-layout
来源: https://codeday.me/bug/20191003/1849083.html
java设置imageview图片大小_java – 在android中设置imageview相关推荐
- java改变背景图片大小_java编写界面设置 背景图片的大小
设置背景图片大小跟电脑屏幕大小一致: 方法: ImageIcon background = new ImageIcon("res\\index.jpg"); Dimension s ...
- android imageview 图片切换动画,在Android中以动画方式将ImageView移动到不同的位置...
TranslateAnimation animation = new TranslateAnimation(0, 50, 0, 100); animation.setDuration(1000); a ...
- java fileitem 识别图片大小_Java FileItem.getSize方法代碼示例
本文整理匯總了Java中org.apache.commons.fileupload.FileItem.getSize方法的典型用法代碼示例.如果您正苦於以下問題:Java FileItem.getSi ...
- java解析pdf 图片文字_Java 读取PDF中的文本和图片
本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...
- android设置成默认应用程序,在Android中设置和取消设置默认应用
我有一个尝试"进入信息亭模式"的应用程序,但是我只希望它仅出现在一个Activity上.在修改了一些控件之后,我想到了Intent.createChooser(). 我想做的是,一 ...
- android thumb大小,Android 设置thumb图片大小
xml: android:thumb="@drawable/seekbar_thumb" seekbar_thumb.xml: 修改为: private int seekWidth ...
- android设置src大小不改变,ImageButton设置src图片大小
需求&起因 有时候没有找到合适大小的资源,需要修改图片大小而且不改变ImageButton的大小.可以通过缩放图片大小的方式改变外观. ImageView的属性android:scaleTyp ...
- ImageButton设置src图片大小
需求&起因 有时候没有找到合适大小的资源,需要修改图片大小而且不改变ImageButton的大小.可以通过缩放图片大小的方式改变外观. ImageView的属性android:scaleTyp ...
- java怎么让图片自适应_Java使背景图片自适应窗体的办法
添加一个面板,窗体布局设置为BorderLayout.center或者null都可以.在面板上进行重绘的时候,调用用 面板.getsize().getHeight和getWidth方法来设置背景图片大 ...
最新文章
- 网络推广外包——网络推广外包专员如何做好网站首页设计
- 趣谈网络协议笔记-二(第五讲)
- Spring Cloud(二) Consul 服务治理实现
- 进程共享(读时共享写时复制)
- leetcode326. 3的幂 如此6的操作你想到了吗
- LeetCode题库整理【Java】—— 2 两数相加
- linux下tomcat启动后出现多个java进程
- 隐马尔可夫HMM(EM算法(期望最大化算法)
- WPF ImageButton
- c语言如何做一个打卡的程序,C语言实现学生打卡系统
- 离线下载Express 2015 for Windows 10
- smartsvn smartgit 安装 及其破解
- 如何用10分钟做出一个表情包视频
- 华为鲲鹏泰山服务器系统安装,鲲鹏处理器正式商用:浙江移动营业厅用上华为泰山服务器...
- 在ipad上的几款远程桌面工具使用体会
- ppt画图画不下——调整ppt页面的大小
- python实现决策树 西瓜书_朴素贝叶斯python代码实现(西瓜书)
- [笔记]光照系统 实时GI、烘焙GI
- 【烈日炎炎战后端】MySQL编程(3.6万字)
- ZOJ 3380 Patchouli's Spell Cards [基础概率DP+大数]
热门文章
- C/C++内存问题检查利器—Purify (二)
- CodeForces 698A - Vacations (Codeforces Round #363 (Div. 2))
- Linux技术研究-基础篇(启动和自动挂载)
- 公司绝不会告诉你的20大秘密
- 【LOJ#6036】[雅礼集训2017Day4]编码
- python资源库——socket网络编程
- BZOJ 1878 hh的项链(简单莫队)
- javascript 将毫秒值转换为天-小时-分钟-秒钟
- 我学习的第一个uiautomator从创建到运行结束
- 数据库设计 Step by Step (2)——数据库生命周期