如何设置在调整大小

如何设置在调整大小

问题描述:

不同的Widget图标我有不同的屏幕尺寸放在文件夹绘制-MDPI /绘制-xhdpi等一个widget图标
如何设置在调整大小

比方说设备有MDPI屏幕,因此Android将使用drawable-mdpi/widget_pic.png [48x48px]

显示我1x1小部件,当我调整窗口小部件2x2它会使用相同的图标,但将会把在中心无需缩放。
我想让android看到小部件的大小变成了2倍大,并相应地从资源中选择了一个图标(即drawable-xhdpi/widget_pic.png[96x96px])。

所以我的问题是如何做到这一点?
我可以想出一个解决方案,但它似乎有缺陷:
捕获onAppWidgetOptionsChanged()事件,并有替换layout/image_src。
但是我不知道从哪里得到2x图标。


的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example"> 
    <application> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
      </intent-filter> 
      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/myWidgetXml"/> 
     </receiver> 
    </application> 
</manifest> 

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/myLayout" 
    android:initialLayout="@layout/myLayout" 
    android:minHeight="40dp" 
    android:minWidth="40dp" 
    android:resizeMode="horizontal" 
    android:updatePeriodMillis="86400000" 
    android:widgetCategory="home_screen"> 
</appwidget-provider> 

myLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin" 
    android:layout_margin="0dp" 
    android:gravity="center" 
    android:background="@android:drawable/alert_dark_frame" 
    android:orientation="vertical"> 

    <ImageButton 
     android:id="@+id/WidgetButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:src="@drawable/widget_pic" /> 
    <TextView 
     android:id="@+id/WidgetTextView" 
     android:textColor="@android:color/white" 
     android:textSize="14sp" 
     android:layout_marginTop="6dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 
</LinearLayout> 

MyWidget.java

public class MyWidget extends AppWidgetProvider { 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     for (int appWidgetId : appWidgetIds) 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
    } 

    private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) { 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout); 
     Intent intent = new Intent(context, MainActivity.class); 
     Uri.Builder builder = new Uri.Builder(); 
     intent.setData(builder.build()); 
     intent.setAction(Intent.ACTION_VIEW); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 
     views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic); 
     views.setTextViewText(R.id.WidgetTextView, "text"); 
     _appWidgetManager.updateAppWidget(WidgetId, views); 
    } 
} 
+0

把相同的分辨率图标(最高分辨率)中的所有文件夹..没关系.. –

+0

@GokulNC,我喜欢这个主意,但最大尺寸在我的应用程序中的图标是800x800px,我想它不会在'ldpi'上看起来很清晰:) –

实际上,它不可能告诉机器人改变资源在运行时
,因为它的应用程序在安装过程中加载它们
因此,有两种选择:

  • 使用android:scaleType="fitCenter"缩放图像和android:adjustViewBounds="true"
  • 有2个资源中的每个drawable- *文件夹
    (对我来说这将是widget_pic1x1.pngwidget_pic2x2.png