Android自定义TextView实现无限制跑马灯效果

 更新时间:2016年10月2日 16:23  点击:1417
本文章为各位介绍一篇关于Android自定义TextView实现无限制跑马灯效果,希望文章能够帮助到各位朋友,有兴趣的朋友可以一起来看看吧。

在做APP开发的时候,有些标题需要实现跑马灯的效果,如果使用系统自带的TextView实现这样的效果,只需要在TextView的属性中添加如下代码:

android:ellipsize="marquee"//跑马灯效果
android:marqueeRepeatLimit="marquee_forever"//无限制不间断显示
android:singleLine="true"//单行显示

但是这样子有一个致命的缺点,就是这种状态的跑马灯只能在TextView处于焦点状态的时候,它才会滚动,对于APP实际的开发应用中很不实用,因为它不可能一直都处于获取焦点状态。

为了是跑马灯无论在什么情况下都能跑起来,达到我们想要的效果,我们需要自定义一个TextView继承TextView,并且重写isFocuse()方法,让它永远返回true,这样跑马灯效果就能一直的跑起来了。

代码如下:

package zm.marqueetextview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class MarqueeTextView extends TextView {

    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

然后直接在布局文件中引用即可:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <zm.marqueetextview.MarqueeTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="这是一个自定义的带有跑马灯效果的TextView这是一个自定义的带有跑马灯效果的TextView" />

</RelativeLayout>

RelativeLayout控件在android开发应用到非常的多了,下文我们就来看一篇关于Android动态设置RelativeLayout控件的高度例子,具体的代码如下所示。

在做项目的时候,其中一个需求是要求banner图的宽和高是1:1,所以我需要获取手机屏幕的宽度,然后动态的把高度值设置为手机屏幕宽度的值,在这项目中就是设置RelativeLayout的高度值,代码如下:

mRllayoutBanner = (RelativeLayout) findViewById(R.id.rl_banner);
WindowManager wm = this.getWindowManager();
int width = wm.getDefaultDisplay().getWidth();//获取屏幕宽度
android.view.ViewGroup.LayoutParams pp = mRllayoutBanner.getLayoutParams();
mRllayoutBanner.getLayoutParams();
pp.height = width;
mRllayoutBanner.setLayoutParams(pp);//把banner图的高度设为此宽度值


当然,其中的width也可以是其他int值,这样就实现了动态设置RelativeLayout的高度。

RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离

EditText的android:hint
设置EditText为空时输入框内的提示信息。
android:gravity 
android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右
android:layout_gravity
android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右

android:layout_alignParentRight
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
** 要注意一点,Drawable文件夹里面的图片命名是不能大写的。

BottomNavigationBar的例子小编网上找了一下不少哦,下面给各位整理了一篇关于BottomNavigationBar的使用例子,具体的如下所示。

这次写个新的底部栏实现方式,BottomNavigationBar,貌似上个月谷歌发布的,使用起来也很方便下面简单介绍下使用方式,

1、compile ‘com.ashokvarma.android:bottom-navigation-bar:0.9.5’     在gradle中加入这句

2、布局文件


<pre><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chenxu.mybottomdemo.MainActivity">
<LinearLayout
    android:id="@+id/ll_root"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom_bar"
    />
    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true" />
 
</RelativeLayout></pre>

3、Java代码


<pre>package com.example.chenxu.mybottomdemo;
 
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
 
 
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
    private BottomNavigationBar mBottomBar;
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;
    private FragmentThree fragmentThree;
    private FragmentFore fragmentFore;
    private FragmentFive fragmentFive;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
    }
 
    /**
     * 添加页面
     */
    private void assignViews() {
        mBottomBar = (BottomNavigationBar) findViewById(R.id.bottom_bar);
        mBottomBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "商品"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "分类"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "购物车"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "我的"))
                .initialise();
        mBottomBar.setTabSelectedListener(this);//设置监听
        setDefaultFragment();//设置默认选项
    }
    private void setDefaultFragment() {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        fragmentOne=new FragmentOne();
        fragmentTransaction.add(R.id.ll_root,fragmentOne);
        fragmentTransaction.commit();
    }
 
    /**
     * 隐藏fragment
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (fragmentOne != null) {
            transaction.hide(fragmentOne);
        }
        if (fragmentTwo != null) {
            transaction.hide(fragmentTwo);
        }
        if (fragmentThree != null) {
            transaction.hide(fragmentThree);
        }
        if (fragmentFore != null) {
            transaction.hide(fragmentFore);
        }
        if (fragmentFive != null) {
            transaction.hide(fragmentFive);
        }
    }
 
    @Override
    public void onTabSelected(int position) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (position){
            case 0:
                if (fragmentOne==null){
                    fragmentOne=new FragmentOne();
                    fragmentTransaction.add(R.id.ll_root,fragmentOne);
 
                }else {
                    fragmentTransaction.show(fragmentOne);
                }
                break;
            case 1:
                if (fragmentTwo==null){
                    fragmentTwo=new FragmentTwo();
                    fragmentTransaction.add(R.id.ll_root,fragmentTwo);
 
                }else {
                    fragmentTransaction.show(fragmentTwo);
                }
                break;
            case 2:
                if (fragmentThree==null){
                    fragmentThree=new FragmentThree();
                    fragmentTransaction.add(R.id.ll_root,fragmentThree);
 
                }else {
                    fragmentTransaction.show(fragmentThree);
                }
                break;
            case 3:
                if (fragmentFore==null){
                    fragmentFore=new FragmentFore();
                    fragmentTransaction.add(R.id.ll_root,fragmentFore);
 
                }else {
                    fragmentTransaction.show(fragmentFore);
                }
                break;
            case 4:
                if (fragmentFive==null){
                    fragmentFive=new FragmentFive();
                    fragmentTransaction.add(R.id.ll_root,fragmentFive);
 
                }else {
                    fragmentTransaction.show(fragmentFive);
                }
                break;
        }
        fragmentTransaction.commit();
    }
 
    @Override
    public void onTabUnselected(int position) {
 
    }
 
    @Override
    public void onTabReselected(int position) {
 
    }
}</pre>

注:每个底部按钮对应一个fragment即可,以上就是全部内容,用起来效果不错,推荐使用

本文章来为各位介绍在安卓开发中碰到关于 Fresco加载静态图片和动态图片的应用了,这个例子非常的简单并且有用,各位来看看吧。

之前写项目的时候用过UniversalImageLoader,picasso等图片加载框架,发现他们并不支持加载gif动图,之后了解到了Fresco图片加载框架,它可以加载gif动图,总结了一下它的简单使用。

首先引用,Android Studio的开发者在dependencies中加入一行代码即可:

compile 'com.facebook.fresco:fresco:0.9.0+'

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--不支持wrap_content-->
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/user_avator1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:placeholderImage="@mipmap/ic_launcher" />

    <!--fresco:placeholderImage="@mipmap/ic_launcher"是占位符图片-->

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/user_avator2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:placeholderImage="@mipmap/ic_launcher" />
</LinearLayout>

Activity:

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(MainActivity.this);//注册,在setContentView之前。
        setContentView(R.layout.activity_main);
        SimpleDraweeView simpleDraweeView1 = (SimpleDraweeView) findViewById(R.id.user_avator1);
        SimpleDraweeView simpleDraweeView2 = (SimpleDraweeView) findViewById(R.id.user_avator2);
        Uri uri = Uri.parse("http://s1.dwstatic.com/group1/M00/9B/31/468f0fc71e49479bf021c64cefee2882.gif");
        //加载静态图片
        simpleDraweeView1.setImageURI(uri);
        //加载动态图片
        DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(uri).setAutoPlayAnimations(true).build();
        simpleDraweeView2.setController(controller);

    }
}

[!--infotagslink--]

相关文章

  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
  • C#创建自定义控件及添加自定义属性和事件使用实例详解

    这篇文章主要给大家介绍了关于C#创建自定义控件及添加自定义属性和事件使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • Android模拟器上模拟来电和短信配置

    如果我们的项目需要做来电及短信的功能,那么我们就得在Android模拟器开发这些功能,本来就来告诉我们如何在Android模拟器上模拟来电及来短信的功能。 在Android模拟...2016-09-20
  • 夜神android模拟器设置代理的方法

    夜神android模拟器如何设置代理呢?对于这个问题其实操作起来是非常的简单,下面小编来为各位详细介绍夜神android模拟器设置代理的方法,希望例子能够帮助到各位。 app...2016-09-20
  • android自定义动态设置Button样式【很常用】

    为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
  • Android WebView加载html5页面实例教程

    如果我们要在Android应用APP中加载html5页面,我们可以使用WebView,本文我们分享两个WebView加载html5页面实例应用。 实例一:WebView加载html5实现炫酷引导页面大多...2016-09-20
  • 深入理解Android中View和ViewGroup

    深入理解Android中View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。本教程我们深...2016-09-20
  • Android自定义WebView网络视频播放控件例子

    下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02
  • 自定义jquery模态窗口插件无法在顶层窗口显示问题

    自定义一个jquery模态窗口插件,将它集成到现有平台框架中时,它只能在mainFrame窗口中显示,无法在顶层窗口显示. 解决这个问题的办法: 通过以下代码就可能实现在顶层窗口弹窗 复制代码 代码如下: $(window.top.documen...2014-05-31
  • Android用MemoryFile文件类读写进行性能优化

    java开发的Android应用,性能一直是一个大问题,,或许是Java语言本身比较消耗内存。本文我们来谈谈Android 性能优化之MemoryFile文件读写。 Android匿名共享内存对外A...2016-09-20
  • Android设置TextView竖着显示实例

    TextView默认是横着显示了,今天我们一起来看看Android设置TextView竖着显示如何来实现吧,今天我们就一起来看看操作细节,具体的如下所示。 在开发Android程序的时候,...2016-10-02
  • 自定义feignClient的常见坑及解决

    这篇文章主要介绍了自定义feignClient的常见坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-20
  • pytorch 自定义卷积核进行卷积操作方式

    今天小编就为大家分享一篇pytorch 自定义卷积核进行卷积操作方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-05-06
  • android.os.BinderProxy cannot be cast to com解决办法

    本文章来给大家介绍关于android.os.BinderProxy cannot be cast to com解决办法,希望此文章对各位有帮助呀。 Android在绑定服务的时候出现java.lang.ClassCastExc...2016-09-20
  • Android 实现钉钉自动打卡功能

    这篇文章主要介绍了Android 实现钉钉自动打卡功能的步骤,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下...2021-03-15
  • Android 开发之布局细节对比:RTL模式

    下面我们来看一篇关于Android 开发之布局细节对比:RTL模式 ,希望这篇文章对各位同学会带来帮助,具体的细节如下介绍。 前言 讲真,好久没写博客了,2016都过了一半了,赶紧...2016-10-02
  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则

    YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规则后才能被提交,可以很有效地保证...2015-11-24
  • Vue 组件复用多次自定义参数操作

    这篇文章主要介绍了Vue 组件复用多次自定义参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-27