我setImageBitmap
从网址到imageview。我只想定义高度,我希望应用程序按比例自动设置宽度。
这是我的代码:
URL imageURL = new URL("url");
HttpURLConnection connection = (HttpURLConnection) imageURL.openConnection();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
image_projet.setImageBitmap(bitmap);
答案 0 :(得分:0)
float aspect = bitmap.getWidth() / (float) bitmap.getHeight()
int newWidth = newHeight * aspect;
你自己可以计算新的宽度。
答案 1 :(得分:0)
我仍然是Android的初学者。我很乐意接受批评。但这是我的答案。第一个答案是对的。以下是我在TextView上应用它的示例。
TextView name = (TextView)findViewById(R.id.name);
//this is where I got my bitmap. Not complete. This is inside AsyncTask
byte[] decodedString = Base64.decode(getBase64(jsonResult), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
/////
//here is His Code
int newHeight = 300; //I added this
float aspect = bitmap.getWidth() / (float) bitmap.getHeight() ;
float newWidth = newHeight * aspect;
//Now apply the it
name.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(getResources(),Bitmap.createScaledBitmap(bitmap,(int)newWidth,newHeight,true)), null, null);
无论如何,如果你觉得很难理解,这里是完整的源代码
MainActivity.java
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
private ProgressDialog pd;
String jsonResult="";
TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView)findViewById(R.id.name);
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context
.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnected()){
JasonReadTask jasonReadTask = new JasonReadTask();
String url = "http://192.168.42.164/red_bank/android_connector/get_by_id.php?q=1"; //replace this URL accordingly
jasonReadTask.execute(url);
}
else{
Toast.makeText(this,"Not Connected", Toast.LENGTH_LONG).show();
}
}
private class JasonReadTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute(){
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Please wait");
pd.setIndeterminate(false);
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
try{
HttpResponse httpResponse = httpClient.execute(httpPost);
jsonResult = inputStreamToString(httpResponse.getEntity().getContent()).toString();
} catch (IOException e){
Log.e("doInBackground", "IOException: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result){
pd.dismiss();
byte[] decodedString = Base64.decode(getBase64(jsonResult), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
float aspect = decodedByte.getWidth() / (float) decodedByte.getHeight() ;
float newWidth = 300 * aspect;
name.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(getResources(),Bitmap
.createScaledBitmap(decodedByte,(int)newWidth,300,false)), null, null);
}
}
/*
* Converts InputStream to String builder
* for conversion to String.
*/
private StringBuilder inputStreamToString(InputStream is){
String rLine = "";
//Converts Buffered data to String
StringBuilder stringBuilder=new StringBuilder();
//Reads the JSON data line by line
BufferedReader rd=new BufferedReader(new InputStreamReader(is));
try {
while ((rLine=rd.readLine()) != null) {
stringBuilder.append(rLine); //each line is appended into one file
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return stringBuilder;
}
/*
* Get the base64 encoded image
* and return it for Bitmap to ImageView Conversion
*/
private String getBase64(String result){
String base64="";
try{
JSONObject jsonObject = new JSONObject(result); //Get the Entire Object
JSONArray jsonArray = jsonObject.optJSONArray("accounts"); //Interpret the Object
// into array
JSONObject jasonData = jsonArray.getJSONObject(0); //Get specific row
base64 = jasonData.optString("profile"); //getSpecific data
name.setText(jasonData.optString("full_name"));
} catch(JSONException e){
e.getMessage();
Log.e("getBase64", "JSONException: " + e.getMessage());
}
return base64;
}
}
这是布局
<!--activity_main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:text="Not Connected"
android:id="@+id/name" android:layout_gravity="center_horizontal" android:textSize="24sp" />
</LinearLayout>