温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android实现透明指示层

发布时间:2020-08-02 00:27:31 来源:网络 阅读:853 作者:pastwinder 栏目:移动开发

以下仅为自己工作记录,方便今后查阅

第一步:从原理上,了解这个功能,我们使用RelativeLayout 布局,然后在主页面内容之后,直接加入指示层如下代码。

<RelativeLayout android:orientation="vertical"android:background="#ffe4e4e4" android:layout_width="fill_parent"android:layout_height="fill_parent"

  xmlns:android="http://schemas.android.com/apk/res/android">

  <LinearLayout android:orientation="vertical"android:background="#ffe4e4e4" android:layout_width="fill_parent"android:layout_height="fill_parent">

    <include layout="@layout/hotel_details_head" />

    

    <ListView android:id="@id/hotel_detail_rooms"android:fadingEdgeLength="5.0dip" android:layout_width="fill_parent"android:layout_height="wrap_content" android:listSelector="#00000000"android:cacheColorHint="#00000000" android:divider="#00ffffff"android:dividerHeight="1.0dip" />

    

   

    </LinearLayout>

    

    <ImageView   

    android:src="@drawable/tip1"  

    android:layout_height="fill_parent"   

    android:layout_width="fill_parent"  

    android:id="@+id/helpTipbackground"  

    

    android:scaleType="centerInside"

    android:keepScreenOn="true"  

    android:visibility="gone"

  />

    <ImageView   

    android:src="@drawable/thisred"  

    android:layout_height="wrap_content"   

    android:layout_width="wrap_content"  

    android:id="@+id/helpTip"  

    android:scaleType="center"

    android:visibility="gone"

  />

</RelativeLayout>

解释:linearlayout是主要显示的内容,而下面的两个p_w_picpathview是用来指示的图层。这里我使用指示层和解释层分开的方式,因为我发现android不同尺寸的屏幕如果只用一层太不好弄啦,也可以合并到一层根据个人需求自行决定,如果是一层那就不需要两个p_w_picpathview了。我们可以先将它们隐藏( android:visibility="gone"),等到主页面正常显示出来在将指示层显示出来。


第二步:添加activity代码,实现点击图层变化解释层的图标和指示层的位置。


private ImageView helpTipIV;//手指

private ImageView helpTipBackIV;//手指背景

private int backpicture=0;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_hotel_detail);

helpTipIV=(ImageView)this.findViewById(R.id.helpTip);

helpTipIV.setOnClickListener(this);

int[]xy = new int[2];

hotel_detail_save_iconIV.getLocationOnScreen(xy);

int x=xy[0];

int y=xy[1];

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300,350);

params.setMargins(x-90, y+20, 0, 0);

helpTipIV.setLayoutParams(params);

helpTipBackIV=(ImageView)this.findViewById(R.id.helpTipbackground);

 

helpTipBackIV.setOnClickListener(this);

hotel_details_locationTV= (LinearLayout)hotelroomheader.findViewById(R.id.hotel_details_location);

}


@Override

public void onClick(View view) {

// TODO Auto-generated method stub

   int i=view.getId();

            switch(view.getId())

{

case R.id.helpTipbackground:{

if(backpicture==0)

{

 

helpTipBackIV.setImageResource(R.drawable.tip2);

int[]xy = new int[2];

hotel_details_locationTV.getLocationOnScreen(xy);

int x=xy[0];

int y=xy[1];

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300,350);

params.setMargins(x, y+20, 0, 0);

helpTipIV.setLayoutParams(params);

backpicture++;

}else

{

helpTipBackIV.setVisibility(View.GONE);

helpTipIV.setVisibility(View.GONE);

}

helpTipBackIV.setImageResource(R.drawable.tip2);

break;

}

}

}

解释:在初始化界面成功后,先给手指层定义第一个指示的位置,然后给背景层添加点击事件,这里的onclick()方法是定义在父类中的,也可以直接为p_w_picpathview控件添加点击事件。这里要强调一下,原来我用的给控件定位的方法是setLeft()、setTop()、setRight()、setBottom()。这个方式如果对应一个图层的时候没有问题,但是对应两层的时候,如果其中一层调用了setImageResource()方法,另一侧会莫名其妙的变回到初始位置,由于我也是菜鸟不是很了解其中具体的原因,大概可能就是因为setImageResource()方法会重新初始化下另一个p_w_picpathview吧,总之用RelativeLayout.LayoutParams 是比较稳妥的方法。


这样运行之后,界面一开始没有指示层,直到显示出需要的界面后,指示层才出现,指示层会在需要解释的功能按钮旁边起到指示的作用,而解释层则会覆盖全部界面,当点击覆盖全部界面的解释层的时候,指示层改变位置,解释层更换下一张解释图片,OK大功告成。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI