fixed aspect ratio of large profile image in user profile

This commit is contained in:
Mariotaku Lee 2014-12-14 12:09:38 +08:00
parent fface0d9f2
commit 97b8487c56
7 changed files with 47 additions and 21 deletions

View File

@ -29,6 +29,7 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Matrix.ScaleToFit;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
@ -313,8 +314,36 @@ public class CircularImageView extends ImageView {
Shader.TileMode.CLAMP);
mMatrix.reset();
switch (getScaleType()) {
case CENTER_CROP: {
final float srcRatio = source.width() / source.height();
final float dstRatio = dest.width() / dest.height();
if (srcRatio > dstRatio) {
// Source is wider than destination, fit height
tempDest.top = dest.top;
tempDest.bottom = dest.bottom;
final float dstWidth = dest.height() * srcRatio;
tempDest.left = dest.centerX() - dstWidth / 2;
tempDest.right = dest.centerX() + dstWidth / 2;
} else if (srcRatio < dstRatio) {
tempDest.left = dest.left;
tempDest.right = dest.right;
final float dstHeight = dest.width() / srcRatio;
tempDest.top = dest.centerY() - dstHeight / 2;
tempDest.bottom = dest.centerY() + dstHeight / 2;
} else {
tempDest.set(dest);
}
break;
}
default: {
tempDest.set(dest);
break;
}
}
// Fit bitmap to bounds.
mMatrix.setRectToRect(source, dest, Matrix.ScaleToFit.FILL);
mMatrix.setRectToRect(source, tempDest, ScaleToFit.CENTER);
shader.setLocalMatrix(mMatrix);
mBitmapPaint.setShader(shader);
@ -323,6 +352,8 @@ public class CircularImageView extends ImageView {
}
private RectF tempDest = new RectF();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static class CircularOutlineProvider extends ViewOutlineProvider {
@Override

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="0.2dp"/>
<solid android:color="@color/divider_color"/>
</shape>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="0.2dp"/>
<solid android:color="@color/divider_color"/>
</shape>

View File

@ -16,8 +16,17 @@
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_action_assist_twidere_normal" android:state_active="false" android:state_enabled="true" android:state_focused="false"/>
<item android:drawable="@drawable/ic_action_assist_twidere_activated" android:state_active="true" android:state_enabled="true" android:state_focused="false"/>
<item android:drawable="@drawable/ic_action_assist_twidere_activated" android:state_active="false" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/ic_action_assist_twidere_normal"
android:state_active="false"
android:state_enabled="true"
android:state_focused="false"/>
<item android:drawable="@drawable/ic_action_assist_twidere_activated"
android:state_active="true"
android:state_enabled="true"
android:state_focused="false"/>
<item android:drawable="@drawable/ic_action_assist_twidere_activated"
android:state_active="false"
android:state_enabled="true"
android:state_focused="true"/>
</selector>

View File

@ -233,7 +233,7 @@
android:layout_width="match_parent"
android:layout_height="@dimen/action_button_size"
android:baselineAligned="false"
android:divider="@drawable/divider_vertical_small_holo"
android:divider="?android:dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
android:splitMotionEvents="false">

View File

@ -22,6 +22,7 @@
</style>
<style name="Widget.ProfileType" parent="Widget.Base">
<item name="android:scaleType">centerInside</item>
<item name="android:elevation">@dimen/elevation_card</item>
<item name="android:outlineProvider">none</item>
</style>

View File

@ -144,5 +144,6 @@
</style>
<style name="Widget.ProfileType" parent="Widget.Base">
<item name="android:scaleType">centerInside</item>
</style>
</resources>