【问题标题】:Is it possible to add custom drawable to imageView in an Android Home Screen Widget?是否可以在 Android 主屏幕小部件中向 im​​ageView 添加自定义可绘制对象?
【发布时间】:2023-03-12 00:09:01
【问题描述】:

我正在为 android 做一个主屏幕小部件,我想在小部件上使用 Lottie 动画。我知道不支持LottieAnimationView,因为该小部件仅支持某些视图。但是我可以使用这个自定义可绘制LottieDrawable 在小部件上的 ImageView 中使用吗?

【问题讨论】:

    标签: android android-widget android-animation android-imageview lottie


    【解决方案1】:

    不,对不起。您只能使用可绘制资源或位图,而不是 Drawable 对象。

    虽然您的应用具有 Lottie,但您的应用并未呈现应用小部件。主屏幕呈现应用小部件。因此,RemoteViews 系统将您限制为框架的一部分,任何主屏幕都可以使用。

    【讨论】:

    • 我不知道可绘制资源和可绘制对象之间的区别。是否有任何文件证实这一点?谢谢!
    • @PedroRomanoBarbosa:如果您查看the JavaDocs for RemoteViews,您将找不到任何采用Drawable 参数的方法。
    【解决方案2】:

    是的,这是可能的。试试这样的:

    Paint p = new Paint(); 
    p.setAntiAlias(true);
    p.setStyle(Style.STROKE);
    p.setStrokeWidth(8);
    p.setColor(0xFFFF0000);
    
    Bitmap bitmap = Bitmap.createBitmap(100, 100, 
    Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawArc(new RectF(10, 10, 90, 90), 0, 270, false, p);
    
    RemoteViews views = new 
    RemoteViews(updateService.getPackageName(), R.layout.main);
    views.setImageViewBitmap(R.id.canvas, bitmap);
    
    ComponentName componentName = new 
    ComponentName(updateService, 
    DashboardAppWidgetProvider.class);
    AppWidgetManager appWidgetManager = 
    AppWidgetManager.getInstance(updateService);
    appWidgetManager.updateAppWidget(componentName, 
    views);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多