0
我從遠程服務器TextView
內容圖像並將其拉入TextView
,我想使這個下載,或在大模繪製圖像點擊上點擊打開的圖像例如android可點擊監聽器嗎?
如何使Drawable
點擊或具有onclicklistener
?
這是我的班,我用HTML格式
例如,如果TextView
有<img src="http://www.bla.com/1.jpg" />
類下載後得出這樣的形象怎麼可以讓它有點擊監聽器,大模打開它!
'公共類URLImageParser實現ImageGetter {
//Activity Context
Context c;
//TextView Content
TextView container;
//File Cache
FileCache fileCache;
//Imgae Resulotion
int IMGAE_REZ = 100;
/*************************************************
* Setup Class Values
*/
public URLImageParser(Context c,int position,View t) {
this.c = c;
this.container = (TextView) t;
fileCache = new FileCache(c);
}
/*************************************************
* Start Draw
*/
@SuppressWarnings("deprecation")
public Drawable getDrawable(String source) {
//Fix Url Spaces
source = (source != null) ? source.replace(" ", "%20") : null;
//Not cached yet Fetch and Draw
if (source != null)
{
//Check if Cached by Bitmap
Bitmap cached = Api.bitmap_cache.get(source);
//if not in Bitmap Cache get from file cache or Download it
if (cached != null)
{
Drawable drw;
drw = new BitmapDrawable(cached);
drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));
return drw;
}else
{
URLDrawable urlDrawable = new URLDrawable();
// get the actual source
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable , source);
asyncTask.execute(source);
return urlDrawable;
}
}else
{
return null;
}
}
/*******************************************************
* Decode file as Bitmap
* Return Bitmap Decoded
*/
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<IMGAE_REZ || height_tmp/2<IMGAE_REZ)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
/*******************************************************
* AsyncTask Download or get from cache Drawable
* Setting Image if exists or broken image
* Below this class nothing related to Above
*/
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
//Image Draw
URLDrawable urlDrawable;
//If called from Cache BITMAP
boolean is_cached = false;
//File Cache id in BitmapCache
String cacheID;
//Bitmap to Cache
Bitmap cacheMe;
public ImageGetterAsyncTask(URLDrawable d, String cacheID) {
this.urlDrawable = d;
this.cacheID = cacheID;
if (d == null)
{
cancel(true);
}
}
/*****************************************************
* Get Draw
*/
@Override
protected Drawable doInBackground(String... params) {
return fetchDrawable(params[0]);
}
/*****************************************************
* Draw Image and Set Bounds if not Set
*/
@Override
protected void onPostExecute(Drawable result) {
//if Drawable not null procced
if (result != null)
{
urlDrawable.setBounds(0,0, (int)(result.getIntrinsicWidth()*5.5),(int)(result.getIntrinsicHeight()*5.5));
urlDrawable.drawable = result;
int newhight = (URLImageParser.this.container.getHeight() + (int)(result.getIntrinsicHeight()*5.5));
URLImageParser.this.container.setHeight(newhight);
URLImageParser.this.container.setEllipsize(null);
URLImageParser.this.container.requestLayout();
URLImageParser.this.container.invalidate();
Api.bitmap_cache.put(cacheID, cacheMe);
}
}
/*****************************************************
* Fetch Draw convert from Bitmap to Draw
* Return Draw
*/
@SuppressWarnings("deprecation")
public Drawable fetchDrawable(String urlString) {
Drawable drw = null;
cacheMe = downloadFile(urlString);
if (cacheMe != null)
{
drw = new BitmapDrawable(cacheMe);
drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));
}
return drw;
}
/*******************************************************
* Return Bitmap of Image String if downloaded or Cached
* Return Bitmap
*/
private Bitmap downloadFile (String url)
{
Bitmap bitmap = null;
File f = fileCache.getFile(url);
bitmap = decodeFile(f);
//return from cached file
if (bitmap != null)
{
return bitmap;
}else
{
try {
//Download and Cache file
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();
bitmap = decodeFile(f);
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
{
Api.bitmap_cache.clear();
}
}
}
return bitmap;
}
}
}`
http://stackoverflow.com/a/8086317/826657看到這個* * –