Skip to content

Commit

Permalink
hzuapps#6 hzuapps#71 第6次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Dec 23, 2020
1 parent 3643663 commit 2a1eafa
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 82 deletions.
169 changes: 87 additions & 82 deletions students/net1814080903202/Moment1814080903202Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,89 @@

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;

public class Moment1814080903202Activity extends AppCompatActivity implements View.OnClickListener{
private ListView listView;
private ImageButton back;
private ImageButton scan;
private SimpleAdapter simpleAdapter;
private List<Map<String,Object>> resultlist;
private Gson gson;
private GsonBuilder gsonBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_moment1814080903202);
listView = findViewById(R.id.MomentList);
back = findViewById(R.id.back);
scan = findViewById(R.id.scan);
back.setOnClickListener(this);
scan.setOnClickListener(this);
listView.setAdapter(new MomentAdapter(this));
gsonBuilder=new GsonBuilder();
gson=gsonBuilder.create();
runOnUiThread(new Runnable() {
@Override
public void run() {
String jsonText = getJson();
resultlist = gson.fromJson(jsonText,new TypeToken<List<Map<String,Object>>>(){}.getType());
System.out.println(resultlist);
}
});
simpleAdapter = new SimpleAdapter(Moment1814080903202Activity.this,resultlist,
R.layout.moment_listview_item,
new String[]{"name","time","head","content","img1","img2","img3"},
new int[]{R.id.name,R.id.time,R.id.head,R.id.content,R.id.img1,R.id.img2,R.id.img3});
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view instanceof ImageView) {
ImageView iv = (ImageView) view;
if(iv.getId() == R.id.head){
RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(iv.getContext(),30));
Glide.with(iv.getContext()).load((String) data).apply(options).into(iv);
}else{
Glide.with(iv.getContext()).load((String) data).into(iv);
}
return true;
}
return false;
}
});
listView.setAdapter(simpleAdapter);
}

@Override
Expand All @@ -43,87 +102,33 @@ public void onClick(View v) {
break;
}
}
}

class MomentAdapter extends BaseAdapter{
private Context mContext;
private LayoutInflater layoutInflater;
public MomentAdapter(Context context){
mContext = context;
layoutInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return 2;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder{
public ImageView head;
public TextView name;
public TextView time;
public TextView content;
public ImageView img1;
public ImageView img2;
public ImageView img3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.moment_listview_item,null);
viewHolder = new ViewHolder();
viewHolder.head = convertView.findViewById(R.id.head);
viewHolder.name = convertView.findViewById(R.id.name);
viewHolder.time = convertView.findViewById(R.id.time);
viewHolder.content = convertView.findViewById(R.id.content);
viewHolder.img1 = convertView.findViewById(R.id.img1);
viewHolder.img2 = convertView.findViewById(R.id.img2);
viewHolder.img3 = convertView.findViewById(R.id.img3);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.head.setBackgroundResource(heads[position]);
viewHolder.name.setText(names[position]);
viewHolder.time.setText(times[position]);
viewHolder.content.setText(contents[position]);
viewHolder.img1.setBackgroundResource(img[0]);
viewHolder.img2.setBackgroundResource(img[1]);
if (position != 1){
viewHolder.img3.setBackgroundResource(img[0]);
private String getJson(){
String jsonurl = "http://49.234.91.116:80/android/Moment.json";
String jsonText = new String();
try {
URL url = new URL(jsonurl);
// 创建网络连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
// 以流的方式去读取网络数据:字符流 / 字节流
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = URLDecoder.decode(lines, "utf-8");
sb.append(lines);
}
System.out.println(sb);
jsonText = sb.toString();
reader.close();
// 断开连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}

return convertView;
return jsonText;
}
public Integer[] heads = {
R.drawable.head1,
R.drawable.head2,

};
public String[] names = {
"Lunette",
"viices",
};
public String[] times = {
"2小时前",
"3小时前",
};
public String[] contents = {
"123",
"456"
};
public Integer[] img = {
R.drawable.head1,
R.drawable.head2,

};
}
}
53 changes: 53 additions & 0 deletions students/net1814080903202/RoundTransform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.hzuapps.androidlabs.net1814080903202;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;

public class RoundTransform extends BitmapTransformation {
private static float radius = 0f;
public RoundTransform(Context context, int dp) {
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}

private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}

public String getId() {
return getClass().getName() + Math.round(radius);
}

@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {

}
}

0 comments on commit 2a1eafa

Please sign in to comment.