-
2022-04-22 16:07:15
对调
实现上述功能代码很简单,如下:
private ObjectAnimator leftAnimator, rightAnimator; private void changeText(View tvFrom, View tvTo) { float distance = tvTo.getX() - tvFrom.getX(); float textWidth = tvFrom.getWidth() - tvTo.getWidth(); if (distance < 0) { distance = Math.abs(distance); float otherDistance = distance - Math.abs(textWidth); if (textWidth > 0) { otherDistance = distance + textWidth; } leftAnimator = ObjectAnimator.ofFloat(tvFrom, "translationX", otherDistance, 0); rightAnimator = ObjectAnimator.ofFloat(tvTo, "translationX", -distance, 0); } else { float otherDistance = distance + Math.abs(textWidth); if (textWidth > 0) { otherDistance = distance - textWidth; } leftAnimator = ObjectAnimator.ofFloat(tvFrom, "translationX", 0, distance); rightAnimator = ObjectAnimator.ofFloat(tvTo, "translationX", 0, -otherDistance); } leftAnimator.setDuration(500); leftAnimator.start(); rightAnimator.setDuration(500); rightAnimator.start(); rightAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { startAnimal = false; } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); }
需要注意的是,如果是简单的展示就没什么问题了,但是如果是要对转换后进行操作的话还要想到这个转换其实是将整个控件也掉位置了,不仅仅只是显示效果而已。需要处理对应的逻辑。
更多相关内容 -
android控件上面显示提醒信息
2013-10-10 13:23:26可以在控件上面,显示提醒信息,和简单方便,很通用 -
Android控件之Button
2022-01-06 21:05:56Button控件表示按钮,通过用户点击来执行操作,当Button控件被点击时会触发点击效果。 一、Button按钮控件设置点击事件 ...LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:Button控件表示按钮,通过用户点击来执行操作,当Button控件被点击时会触发点击效果。
一、Button按钮控件设置点击事件
为Button按钮控件设置点击事件的方式主要有三种方式:
(1)在布局文件中在按钮中添加onClick属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ButtonThree"> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Click" android:text="点击按钮" /> </LinearLayout>
在布局文件中添加了onClik的属性,我们可以在活动中定义专门的方法来实现Button控件的点击事件。注意定义的方法名必须与布局文件中定义的属性名一值。
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Fragment extends AppCompatActivity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); btn = (Button) findViewById(R.id.btn); } public void click(View view){ btn.setText("你点了我你要对我负责"); Toast.makeText(Fragment.this, "哈哈哈,你点了我!", Toast.LENGTH_SHORT).show(); } }
(2)使用匿名内部类的方式设置点击事件。
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Fragment extends AppCompatActivity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btn.setText("你点了我你要对我负责"); Toast.makeText(Fragment.this, "哈哈哈,你点了我!", Toast.LENGTH_SHORT).show(); } }); } }
(3)活动中实现OnClickListener接口的方式设置点击事件。
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Fragment extends AppCompatActivity implements View.OnClickListener{ private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); //设置Button控件的点击监听事件 } @Override public void onClick(View v) { btn.setText("被点击了"); Toast.makeText(Fragment.this, "Toast我是轻量级的消息输出", Toast.LENGTH_SHORT).show(); } }
-
Android控件动态显示和隐藏
2022-01-11 20:36:22APP控件属性动画,动态显示隐藏先看效果:
这部分从底部慢慢显示出来
只需两步即可实现此类效果:- 在res中创建文件夹anim,里面放动画效果xml文件
duration - 动画执行多长时间
fromXDelta - X轴方向开始位置,可以是%(自身控件为100%),也可以是像素
toXDelta - X轴方向结束位置,可以是%(自身控件为100%),也可以是像素
fromYDelta - Y轴方向开始位置,可以是%(自身控件为100%),也可以是像素
toYDelta - Y轴方向结束位置,可以是%(自身控件为100%),也可以是像素
startOffset - 延迟多长时间后才开始动画
还有两个属性:
fillBefore 和 fillAfter 这里不进行介绍,只说亲测效果
首先这两个属性放在 set 标签内 ; translate 内无效果
fillBefore 默认属性,显示(由GONE到VISIBLE),隐藏后(由VISIBLE到GONE)消失
fillAfter 显示(由GONE到VISIBLE),隐藏后(由VISIBLE到GONE)不消失,就在最后位置停留显示
隐藏 bottom_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromYDelta="0" android:toYDelta="100%"/> </set>
显示 bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromYDelta="100%" android:toYDelta="0" /> </set>
- 添加动画,预先控件就在你想要的位置,先隐藏掉 visibility=“gone”
然后在显示出来,反之同理
Animation animBottomOut = AnimationUtils.loadAnimation(Activity.this,R.anim.bottom_out); //animBottomOut.setDuration(240);//动态设置多长时间,xml里面设置了,这里就不要写了 view(你要添加效果的控件).setVisibility(VISIBLE); view(你要添加效果的控件).startAnimation(animBottomOut);
- 在res中创建文件夹anim,里面放动画效果xml文件
-
Kotlin自定义android 控件
2022-03-15 17:41:06自定义控件 第一步:自定义layout -> title.xml 编写一个title布局,拥有返回按钮,标题文本,编辑按钮。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=...自定义控件
第一步:自定义layout -> title.xml
编写一个title布局,拥有返回按钮,标题文本,编辑按钮。
<?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="wrap_content" android:background="@drawable/title_bg"> <Button android:id="@+id/titleBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/back_bg" android:text="Back" android:textColor="#fff"/> <TextView android:id="@+id/titleText" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:layout_gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/titleEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#fff"/> </LinearLayout>
第二步:自定义控件 -> TitleLayout.class
创建控件TitleLayout,继承于LinearLayout。
在构造函数中编写逻辑,动态加载布局。
LayoutInflater.from()构建一个LayoutInflater对象,然后调用inflate方法。
参数1:需要加载的布局id。
参数2:加载的布局的父布局。
import android.app.Activity import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import android.widget.* import kotlinx.android.synthetic.main.title.view.* class TitleLayout(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) { init { LayoutInflater.from(context).inflate(R.layout.title, this) titleBack.setOnClickListener { val activity = context as Activity activity.finish() } titleEdit.setOnClickListener { Toast.makeText(context, "Edit...", Toast.LENGTH_LONG).show() } } }
第三步:Activity引用 -> activity_main.xml
<com.lunacattus.customizecontrols.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" />
附录:隐藏activity自带的title
supportActionBar?.hide()
-
Android控件浮在最上层的方法
2021-09-07 09:08:59在XML文件中,可以使用FramLayout或RelativeLayout来布局,每个控件都是覆盖显示的,后加进来的控件覆盖前面的控件。可以把控件写在最后,从而实现显示在最上层的效果。 代码修改 mView.bringToFront(); 可以将布局... -
Android控件的显示(可见、不可见)与隐藏(Java)
2022-04-20 17:35:39Android控件的显示(可见、不可见)与隐藏(Java) -
android控件覆盖方法
2017-10-22 09:33:141.在XML文件中,可以使用FramLayout或RelativeLayout布局,每个控件都是覆盖显示的,后加进来的控件覆盖前面的控件。可以把控件写在最后,从而实现显示在最上层的效果。 2.view.bringToFront() 可以将布局... -
Android控件设置透明度的三种方法
2018-07-27 12:39:59有时会需要改变布局颜色透明度,比如设置控件(如View,Button等)的透明度,有3种方法。 实现方法 java代码实现 text = (TextView) findViewById(R.id.text); text.getBackground().setAlpha(12); ... -
Android 控件缩写
2018-10-16 10:19:12控件 缩写 控件 缩写 TextView txt EditText edt Button btn ImageButton ibtn ImageView img ListView list RadioGroup group RadioButton rbtn ... -
Android控件靠右下
2018-01-18 18:22:12注:设置android:layout_gravity属性, 多个约束使用 | 隔开Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right... -
Android控件的高度,宽度设置
2018-09-09 14:55:37几乎每一个界面控件都需要设置android:layout_height,android:layout_width这两个属性,用于指定该控件的高度和宽度。 fill_parent:表示控件的高或宽与其父容器的高或宽相同。 wrap_content:表示控件的高或宽... -
Android 控件的简单梳理
2022-03-04 10:19:001.Android 的各个控件是怎么控制不同位置的? -
Android控件可见、不可见和隐藏
2020-04-18 11:55:57在xml文件中对控件可进行配置 可见:android:visibility="visible"; 不可见:android:visibility="invisible"; 隐藏:android:visibility="gone"; 在Java代码中进行设置 可见:view.setVisibility(View.VISIBLE); 不... -
Android 控件宽高相等
2019-07-16 10:40:41例示:等宽高的FrameLayout 写一个类去继承...(对控件宽高重新进行测量) public class MyFrameLayout extends FrameLayout { public MyFrameLayout(Context context, AttributeSet attrs, int defStyle... -
android 控件属性大全
2018-06-06 16:26:59“Android控件属性大全”: 关键词:android 控件 属性 大全 控件属性: android属性 android功能强大,界面华丽,但是众多的布局属性就害苦了开发者... -
Android控件显示和隐藏案例详解
2017-08-18 14:52:07本文讲解从Java代码中控制控件的显示和隐藏,让屏幕显示更多想见的内容。以常见的Listview列表控件的显示隐藏为例,讲解具体的实现方法。 -
Android 控件显示在最上层的方法
2018-11-15 10:16:30两种方法: 1.布局中使用FramLayout或RelativeLayout,每个控件都是覆盖显示的, 2代码中.view.bringToFront() 。 -
Android控件 -- 设置组件所占比例
2018-05-18 10:45:331、当我们使用Linearlayout线性布局,放置三个TextView控件,设置android:layout_width属性为wrap_content,并分别设置android:layout_weight比重为1,2,3时: <LinearLayout xmlns:android="... -
Android 控件添加边框线(全部画线,其他边画线)
2017-09-20 11:20:39Android 控件添加边框线(全部画线,其他边画线)本人第一次写博客,写的不好还请指出!一直想写,但是懒,总觉得学过了就记住,孰不知真的会忘!关于边框画线问题网上一直大多数都是一些画全边的,单独的或者其他的很... -
Android 控件顶部阴影如何添加?
2017-04-14 06:05:21 android 5.0md风格貌似做不到吧?谢谢大家帮下 -
一个android控件资源网站
2019-10-08 04:44:56http://www.androidviews.net/ 里面有各种常用控件,赞~ 转载于:https://www.cnblogs.com/pen-ink/p/3186197.html -
Android 控件边框
2018-12-07 11:15:351,在drawable文件夹中右键,new-&amp;amp;amp;gt;drawableresource file,弹出一个...3,下边的代码根据需要修改即可绘制自己想要的圆滑控件: &amp;amp;amp;lt;?xml version=&amp;amp;quot -
Android 控件描边只描一边
2017-02-26 12:07:08“1.0”encoding=“utf-8”?><layer-listxmlns:android=”http://schemas.android.com/apk/res/android”><itemandroid:left=“-2dp”android:right=“-2dp”android:top=“-2dp”><shape><strokeandroid:width= -
Android 控件拖动
2012-03-31 15:21:31Android 控件 拖动 简单 democratic -
Android控件随手指的移动而移动
2018-01-23 15:53:40Android控件随手指的移动而移动 原理:这个不是很难,首先我们要给控件设置触摸监听时间,监听按下,移动,抬起等操作,然后在移动,按下里面分别获取按下的坐标,通过移动获取的坐标减去之前按下的坐标得到移动的... -
App控件定位:Android 控件介绍及元素定位方法
2021-01-14 07:29:01本文将分享Android相关基础知识和Android APP控件定位工具的使用方法。 Android基础知识 Android布局 Android是通过容器的布局属性来管理子控件的位置关系(iOS去掉了布局的概念,直接用变量之间的相对关系完成位置... -
Android控件之EditText(输入文本框控件)
2021-06-06 16:10:28一、EditText控件概述EditText是一个非常重要的组件,可以说他是用户和Android应用进行数据传输窗口有了他就等于有了一扇和Android应用传输的门,通过他用户可以把数据传输给Android应用,从而得到我们想要的数据... -
Android控件设置可点击
2016-05-04 15:45:28看前辈写的代码,有几个控件没有setOnClickLisener还是可以相应点击事件. 他对应的相应点击事件操作写在了xml文件中.现在有看到以下几种方式可以达到上述效果: setOnClickListener(this) + onClick(View view)每一个...