一、认领任务
经过团队讨论,我认领到的工作是:设置圆形头像;
二、任务完成时间估算
界面跳转 | 5h |
效果:
主要的实现代码:
MyFragment.java
package com.example.myapplication5; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.app.Fragment; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import com.alibaba.fastjson.JSON; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class myFragment extends Fragment { private View ly4; private MyHandler handler1; private ImageButton imageButton9; private ImageView imageView2; private ImageButton imageButton12; private ImageButton imageButton13; private TextView textView1; private TextView textView2; private TextView textView3; private TextView textView4; private TextView textView5; private TextView textView6; private TextView textView7; class MyHandler extends Handler { @Override public void handleMessage(Message msg) { // ImageView imageView = (ImageView)ly4.findViewById(R.id.imageView2); // imageView.setImageBitmap((Bitmap)msg.obj); imageView2 = (ImageView) ly4.findViewById(R.id.imageView2); imageView2.setImageBitmap((Bitmap)msg.obj); } } public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { // Restore the fragment's state here } } public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the fragment's state here } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ly4 == null) { Intent intent = getActivity().getIntent(); final String user = intent.getStringExtra("user"); final String Szong = intent.getStringExtra("Szong"); List<user> shopInfos = JSON.parseArray(user, user.class); final String name = intent.getStringExtra("name"); ly4 = inflater.inflate(R.layout.tab04, null); handler1 = new MyHandler(); imageButton9 = (ImageButton) ly4.findViewById(R.id.imageButton9); imageView2 = (ImageView) ly4.findViewById(R.id.imageView2); imageButton12 = (ImageButton) ly4.findViewById(R.id.imageButton12); imageButton13 = (ImageButton) ly4.findViewById(R.id.imageButton13); imageButton9.setImageResource(R.drawable.setting); imageView2.setImageResource(R.drawable.touxiang); imageButton12.setImageResource(R.drawable.daka); imageButton13.setImageResource(R.drawable.ziliao2); textView1 = ly4.findViewById(R.id.textView17); textView2 = ly4.findViewById(R.id.textView27); textView3 = ly4.findViewById(R.id.textView15); textView4 = ly4.findViewById(R.id.textView30); textView5 = ly4.findViewById(R.id.textView19); textView6 = ly4.findViewById(R.id.textView21); textView7 = ly4.findViewById(R.id.textView23); if(null == shopInfos || shopInfos.size() ==0 ){ String id =intent.getStringExtra("id"); String weight =intent.getStringExtra("weight"+"kg"); String height =intent.getStringExtra("height"+"cm"); String sex =intent.getStringExtra("sex"); String year =intent.getStringExtra("year"); String school =intent.getStringExtra("school"); textView1.setText(id); textView2.setText(weight); textView3.setText(height); textView4.setText(""+"秒"); textView5.setText(year); textView6.setText(sex); textView7.setText(school); }else{ for (user user1 : shopInfos) { textView1.setText(user1.getVname()); textView2.setText(user1.getWeight()+"kg"); textView3.setText(user1.getHeight()+"cm"); textView4.setText(Szong+"秒"); textView5.setText(user1.getYear()); textView6.setText(user1.getSex()); textView7.setText(user1.getSchool()); }} imageButton9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), setting.class); String password = getActivity().getIntent().getStringExtra("password"); intent.putExtra("name", name); intent.putExtra("password", password); intent.putExtra("user", user); intent.putExtra("Szong", Szong); startActivity(intent); } }); imageButton12.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), calendard.class); startActivity(intent); } }); new Thread(new Runnable() { @Override public void run() { try { //http://10.0.2.2:8080/system/fileService/images/admin.jpg http://localhost:8080/system/fileService/images/admin.jpg String path = "http://120.79.40.20/system/fileService/images/" + name + ".jpg";//String path = "http://120.79.40.20/system/fileService/images/hua.jpg"; //2:把网址封装为一个URL对象 URL url = new URL(path); //3:获取客户端和服务器的连接对象,此时还没有建立连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //4:初始化连接对象 conn.setRequestMethod("GET"); //设置连接超时 conn.setConnectTimeout(8000); //设置读取超时 conn.setReadTimeout(8000); //5:发生请求,与服务器建立连接 conn.connect(); //如果响应码为200,说明请求成功 if (conn.getResponseCode() == 200) { //获取服务器响应头中的流 InputStream is = conn.getInputStream(); //读取流里的数据,构建成bitmap位图 Bitmap bm = BitmapFactory.decodeStream(is); Message msg = new Message(); msg.obj = bm; handler1.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } return ly4; } }
相应的xml文件:
<com.example.myapplication5.CircleImageView android:id="@+id/imageView2" android:layout_width="79dp" android:layout_height="84dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.021" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.346" app:srcCompat="@drawable/touxiang" />
CircleImageView.java
package com.example.myapplication5; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; public class CircleImageView extends androidx.appcompat.widget.AppCompatImageView{ private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; private static final int COLORDRAWABLE_DIMENSION = 1; private static final int DEFAULT_BORDER_WIDTH = 0; private static final int DEFAULT_BORDER_COLOR = Color.BLACK; private final RectF mDrawableRect = new RectF(); private final RectF mBorderRect = new RectF(); private final Matrix mShaderMatrix = new Matrix(); private final Paint mBitmapPaint = new Paint(); private final Paint mBorderPaint = new Paint(); private int mBorderColor = DEFAULT_BORDER_COLOR; private int mBorderWidth = DEFAULT_BORDER_WIDTH; private Bitmap mBitmap; private BitmapShader mBitmapShader; private int mBitmapWidth; private int mBitmapHeight; private float mDrawableRadius; private float mBorderRadius; private boolean mReady; private boolean mSetupPending; public CircleImageView(Context context) { super(context); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); super.setScaleType(SCALE_TYPE); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0); mBorderWidth = a.getDimensionPixelSize( R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH); mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR); a.recycle(); mReady = true; if (mSetupPending) { setup(); mSetupPending = false; } } @Override public ScaleType getScaleType() { return SCALE_TYPE; } @Override public void setScaleType(ScaleType scaleType) { if (scaleType != SCALE_TYPE) { throw new IllegalArgumentException(String.format( "ScaleType %s not supported.", scaleType)); } } @Override protected void onDraw(Canvas canvas) { if (getDrawable() == null) { return; } canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint); if (mBorderWidth != 0) { canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); setup(); } public int getBorderColor() { return mBorderColor; } public void setBorderColor(int borderColor) { if (borderColor == mBorderColor) { return; } mBorderColor = borderColor; mBorderPaint.setColor(mBorderColor); invalidate(); } public int getBorderWidth() { return mBorderWidth; } public void setBorderWidth(int borderWidth) { if (borderWidth == mBorderWidth) { return; } mBorderWidth = borderWidth; setup(); } @Override public void setImageBitmap(Bitmap bm) { super.setImageBitmap(bm); mBitmap = bm; setup(); } @Override public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); mBitmap = getBitmapFromDrawable(drawable); setup(); } @Override public void setImageResource(int resId) { super.setImageResource(resId); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } private Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } } private void setup() { if (!mReady) { mSetupPending = true; return; } if (mBitmap == null) { return; } mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mBitmapPaint.setAntiAlias(true); mBitmapPaint.setShader(mBitmapShader); mBorderPaint.setStyle(Paint.Style.STROKE); mBorderPaint.setAntiAlias(true); mBorderPaint.setColor(mBorderColor); mBorderPaint.setStrokeWidth(mBorderWidth); mBitmapHeight = mBitmap.getHeight(); mBitmapWidth = mBitmap.getWidth(); mBorderRect.set(0, 0, getWidth(), getHeight()); mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2); mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth); mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2); updateShaderMatrix(); invalidate(); } private void updateShaderMatrix() { float scale; float dx = 0; float dy = 0; mShaderMatrix.set(null); if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) { scale = mDrawableRect.height() / (float) mBitmapHeight; dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f; } else { scale = mDrawableRect.width() / (float) mBitmapWidth; dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f; } mShaderMatrix.setScale(scale, scale); mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth); mBitmapShader.setLocalMatrix(mShaderMatrix); } }
遇到的问题:
头像裁剪没有在中央。