校园信息平台部分页面开发技术文档
程序员文章站
2022-03-23 22:44:38
首先是开发用户注册页面,以继承方式引用部分接口。这部分接口连接到服务器。还处理了注册中部分异常。public class sign_in extends AppCompatActivitytry{ DataOutputStream outt;outt=new DataOutputStream(sc.getOutputStream()); outt.writeUTF(sendmsg);Log.d("输出到服务器完成", "66 ");}catch(Excepti...
首先是开发用户注册页面,以继承方式引用部分接口。这部分接口连接到服务器。还处理了注册中部分异常。
public class sign_in extends AppCompatActivity try{ DataOutputStream outt; outt=new DataOutputStream(sc.getOutputStream()); outt.writeUTF(sendmsg); Log.d("输出到服务器完成", "66 "); }catch(Exception e){ Log.d("输出到服务器失败", "00 "); }
设计屏幕相关的辅助类
这部主要在网上查找已有代码并进行更改。
public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /**
* 获得屏幕高度
*
* @param context
* @return
*/ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /**
* 获得屏幕宽度
*
* @param context
* @return
*/ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /**
* 获得状态栏的高度
*
* @param context
* @return
*/ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } /**
* 获取当前屏幕截图,包含状态栏
*
* @param activity
* @return
*/ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /**
* 获取当前屏幕截图,不包含状态栏
*
* @param activity
* @return
*/ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; } }
设计页面4
页面4是从服务器上下载图片并显示。
public class page4 extends AppCompatActivity { private static final String HOST = "192.168.1.2"; private static final int PORT = 10000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_page4); if(getSupportActionBar()!=null) { getSupportActionBar().setTitle("详情"); } TextView typeText = findViewById(R.id.textview4_1); TextView nameText = findViewById(R.id.textView4_2); TextView mainText = findViewById(R.id.textView4_3); TextView phoneText = findViewById(R.id.textView4_4); final ImageView imgview = findViewById(R.id.imageView4_1); final globalValue gv = (globalValue) getApplication(); Typeface tf4= Typeface.createFromAsset(getAssets(),"fonts/xk.ttf"); // 通过自定义字体生成字体对象 phoneText.setTypeface(tf4); // 将TextView设置自定义字体。 Typeface tf3 = Typeface.createFromAsset(getAssets(),"fonts/kt.ttf"); // 通过自定义字体生成字体对象 mainText.setTypeface(tf3); // 将TextView设置自定义字体。 typeText.setText(gv.globalType.get(gv.getItemindex())); nameText.setText("名称:"+gv.globalName.get(gv.getItemindex())); mainText.setText("描述:\n"+gv.globalMain.get(gv.getItemindex())); phoneText.setText("联系电话:"+gv.globalPhone.get(gv.getItemindex())); final String lookforpicNum=String.valueOf(gv.getItemindex()); Log.d("looklook", lookforpicNum); new Thread(new Runnable() { public void run() { try { Socket sc = new Socket(HOST, PORT); DataOutputStream outputStream=new DataOutputStream(sc.getOutputStream()); String sendmsg=lookforpicNum; Log.d("捕捉到输入", sendmsg); String serMes; try{ DataOutputStream outt; outt=new DataOutputStream(sc.getOutputStream()); outt.writeUTF(sendmsg); Log.d("输出到服务器完成", "66 "); }catch(Exception e){ Log.d("输出到服务器失败", "00 "); } sc.close(); //接收图片 sc = new Socket(HOST, PORT); DataInputStream dataInput = new DataInputStream(sc.getInputStream()); int size = dataInput.readInt(); byte[] data = new byte[size]; int len = 0; while (len < size) { len += dataInput.read(data, len, size - len); } sc.close(); ByteArrayOutputStream outPut = new ByteArrayOutputStream(); Log.d("接收图片", "完成"); final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); while(data==null || bmp==null) { Log.d("www", "wait"); } bmp.compress(Bitmap.CompressFormat.JPEG, 50, outPut); Log.d("bianma图片", "完成"); Handler mainHandler = new Handler(Looper.getMainLooper()); mainHandler.post(new Runnable() { @Override public void run() { //已在主线程中,可以更新UI imgview.setImageBitmap(bmp); Log.d("显示图片", "run: xsxs"); //initViewImg(); } }); //imgview.setImageBitmap(bmp); } catch (Exception e) { System.out.println("服务器连接出错"); e.printStackTrace(); } } }).start(); }
本文地址:https://blog.csdn.net/qq_40340807/article/details/107754441