首页 > 解决方案 > 如何使用服务类中的布局访问 ImageView?

问题描述

我想从我的 Service 类中更改 ImageView 的颜色。但我不知道如何从服务类访问 ImageView。这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:layout_alignParentStart ="true"
        android:layout_centerVertical = "true"
        android:src="@drawable/batman_r"/>

</RelativeLayout>

我想从我的 Service 类中更改 ImageView 的颜色。但我不知道如何从服务类访问 ImageView。我只想 getImageView 并从中获取可绘制性并将其 Tint 设置为我想要的颜色。请他

标签: androidserviceimageview

解决方案


你不应该,事实上你不能从 更改 UI Service,它不是 UI 线程......你必须自己自定义实现边界服务Service,然后从to发送适当的消息Activity,这可以进行视觉更改(在 UI 线程中) . 一些教程在这里

另一种方法是使用LocalBroadcastManager和发送广播,在(在生命周期中注册/注销接收器)中Service监听它,然后更改 UI。关于这里的文章Activity


推荐阅读