OSDN Git Service

Fixes in UsageBarPreference
authorGilles Debunne <debunne@google.com>
Fri, 6 May 2011 21:12:13 +0000 (14:12 -0700)
committerGilles Debunne <debunne@google.com>
Fri, 6 May 2011 21:12:15 +0000 (14:12 -0700)
The widget layout that was used resulted in a clipped usage bar.
It also had a 6dip left offset inherited from the default layout
which adds margin after the text (even if the text is empty here).
Used a custom layout for the whole preference to fix this issue.

Other fixes in the drawing code to prevent accumulative rounding
issues.

Change-Id: Ief9bf85260345355615c9670624d86f0893eb2d2

res/layout/preference_memoryusage.xml
src/com/android/settings/deviceinfo/Memory.java
src/com/android/settings/deviceinfo/PercentageBarChart.java
src/com/android/settings/deviceinfo/UsageBarPreference.java

index 60b2ba8..2882535 100644 (file)
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:gravity="center_vertical"
+    android:paddingRight="?android:attr/scrollbarSize"
+    android:paddingLeft="@*android:dimen/preference_widget_width">
 
-<com.android.settings.deviceinfo.PercentageBarChart
-        xmlns:android="http://schemas.android.com/apk/res/android"
-        xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
+    <com.android.settings.deviceinfo.PercentageBarChart
+        android:id="@+id/percentage_bar_chart"
         android:layout_width="match_parent"
         android:layout_height="32dip"
         android:minHeight="?android:attr/listPreferredItemHeight"
         android:gravity="center_vertical"
-        android:id="@+id/percentage_bar_chart"
-        android:paddingRight="?android:attr/scrollbarSize"
         settings:minTickWidth="6dip"
         settings:emptyColor="@color/memory_avail">
+    </com.android.settings.deviceinfo.PercentageBarChart>
 
-</com.android.settings.deviceinfo.PercentageBarChart>
+</LinearLayout>
index 1e10c58..93b0f83 100644 (file)
@@ -27,9 +27,9 @@ import android.app.DownloadManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.DialogInterface.OnCancelListener;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.DialogInterface.OnCancelListener;
 import android.content.pm.ApplicationInfo;
 import android.content.res.Resources;
 import android.graphics.drawable.ShapeDrawable;
index 7d02301..95973c4 100644 (file)
@@ -71,20 +71,21 @@ public class PercentageBarChart extends View {
 
         final int width = right - left;
 
-        int lastX = left;
+        float lastX = left;
 
         if (mEntries != null) {
             for (final Entry e : mEntries) {
-                final int entryWidth;
-                if (e.percentage == 0f) {
-                    entryWidth = 0;
+                final float entryWidth;
+                if (e.percentage == 0.0f) {
+                    entryWidth = 0.0f;
                 } else {
-                    entryWidth = Math.max(mMinTickWidth, (int) (width * e.percentage));
+                    entryWidth = Math.max(mMinTickWidth, width * e.percentage);
                 }
 
-                final int nextX = lastX + entryWidth;
-                if (nextX >= right) {
-                    break;
+                final float nextX = lastX + entryWidth;
+                if (nextX > right) {
+                    canvas.drawRect(lastX, top, right, bottom, e.paint);
+                    return;
                 }
 
                 canvas.drawRect(lastX, top, nextX, bottom, e.paint);
@@ -92,13 +93,14 @@ public class PercentageBarChart extends View {
             }
         }
 
-        canvas.drawRect(lastX, top, lastX + width, bottom, mEmptyPaint);
+        canvas.drawRect(lastX, top, right, bottom, mEmptyPaint);
     }
 
     /**
      * Sets the background for this chart. Callers are responsible for later
      * calling {@link #invalidate()}.
      */
+    @Override
     public void setBackgroundColor(int color) {
         mEmptyPaint.setColor(color);
     }
index e9909f1..5aeaef5 100644 (file)
@@ -36,17 +36,17 @@ public class UsageBarPreference extends Preference {
 
     public UsageBarPreference(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
-        setWidgetLayoutResource(R.layout.preference_memoryusage);
+        setLayoutResource(R.layout.preference_memoryusage);
     }
 
     public UsageBarPreference(Context context) {
         super(context);
-        setWidgetLayoutResource(R.layout.preference_memoryusage);
+        setLayoutResource(R.layout.preference_memoryusage);
     }
 
     public UsageBarPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        setWidgetLayoutResource(R.layout.preference_memoryusage);
+        setLayoutResource(R.layout.preference_memoryusage);
     }
 
     public void addEntry(float percentage, int color) {