ScrollingImageView 横向滚动的image view

来源:互联网 发布:mac 版cad导入字体库 编辑:程序博客网 时间:2024/05/23 10:38

ScrollingImageView 是一个横向一直滚动的图片,犹如过火车的车窗外效果;大家可以想象一下是个什么样的场景;
网上也有不少的示例,我呢也就照着别人的照着别人说的做,能实现就好。
因为该控件是第三方的,我就不深入研究,毕竟使用场景还是有限的,不关注具体实现,只关注实现的效果。是不是我们想要。
具体使用如下:
一 。配置
1 . 在app_build.gradle 的dependencie添加

     compile 'com.github.Q42:AndroidScrollingImageView:1.2' 

注意sdk最低不的要求 必须高于15
2 .在工程的 build.gradle 的allprojects 添加

 maven { url "https://jitpack.io"}

二 在我们的activity 的xml 里和其他控件一样使用。因为是自己定义的控件,那么我们声明是自己定义所以必须加上这样一句话

  xmlns:scrolling_image_view="http://schemas.android.com/apk/res-auto"

需要设置两个下边属性

//设置滚动的速度 scrolling_image_view:speed="2dp" //设置需要滚动的图片类容            scrolling_image_view:src="@drawable/scrolling_background" 

三 在activity 的代码里使用需要调用者两个方法
scrollingBackground.stop();
scrollingBackground.start();

下面看具体代码
xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:scrolling_image_view="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="horizontal"    tools:context="com.example.administrator.domepoject.MainActivity">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <com.q42.android.scrollingimageview.ScrollingImageView            android:id="@+id/siv"            android:layout_width="match_parent"            android:layout_height="match_parent"            scrolling_image_view:speed="2dp"            scrolling_image_view:src="@drawable/scrolling_background" />        <ImageView            android:layout_marginTop="40dp"            android:src="@drawable/van"            android:id="@+id/image"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </FrameLayout></LinearLayout>

activity

package com.example.administrator.domepoject;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.annotation.SuppressLint;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.webkit.JavascriptInterface;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ImageView;import android.widget.Toast;import com.q42.android.scrollingimageview.ScrollingImageView;public class MainActivity extends AppCompatActivity {    ImageView image;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ScrollingImageView scrollingBackground = (ScrollingImageView) findViewById(R.id.siv);        scrollingBackground.stop();        scrollingBackground.start();        image= (ImageView) findViewById(R.id.image);        one();    }    private void one(){        ObjectAnimator animator1 = ObjectAnimator.ofFloat(image, "translationY", 0, 20f);        ObjectAnimator animator2 = ObjectAnimator.ofFloat(image, "translationX", 0, 200f);        animator2.setStartDelay(2000);        AnimatorSet set = new AnimatorSet();        set.playTogether(animator1, animator2);        set.setDuration(3000);        set.start();    }}

以上就可以达到我们想要的效果。
为了更加美观。我们可以在布局文件里使用帧布局,嵌套多层不同速度的图片,达到更加完美的视觉效果

0 0