如何在应用程序上反复显示图像和视频(来自应用程序资源),而不会粘贴UI

时间:2016-11-15 21:13:15

标签: java android

我需要做一些应用程序,例如幻灯片中的图像和来自设备的fiels的视频。 我尝试了几个项目 - 但没有人可以显示视频。 什么是最好的方法。我只需要重复显示来自设备的图像和视频,而不会阻碍用户界面

我尝试过一些项目: https://github.com/daimajia/AndroidImageSlider https://github.com/marvinlabs/android-slideshow-widget

谢谢

1 个答案:

答案 0 :(得分:1)

我正在显示视频列表: Activity代码:

public class VideoListActivity extends AppCompatActivity {

    private ListView lvVideos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);

        lvVideos = (ListView)findViewById(R.id.lvVideos);
        lvVideos.setAdapter(new VideosListAdapter(this));

    }
}

适配器代码:

public class VideosListAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;

    private ArrayList<VideoData> listVideoData = new ArrayList<>();
    private String[] creationDates = new String[0];

    public VideosListAdapter(VideoListActivity videoListActivity) {
        inflater = (LayoutInflater) videoListActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // here is a listVideoData  and the creationDates  initialisation code.
    }

    @Override
    public int getCount() {
        return listVideoData.size();
    }

    @Override
    public Object getItem(int position) {
        return listVideoData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) {
            vi = inflater.inflate(R.layout.row_video, null);
        }
        VideoData videoData = listVideoData.get(position);
        VideoView videoView = (VideoView)vi.findViewById(R.id.videoView);
        videoView.setVideoPath(videoData.getVideoFilePath());

        TextView tvDate = (TextView) vi.findViewById(R.id.tvDate);
        tvDate.setText(creationDates[position]);

        TextView tvInfo = (TextView) vi.findViewById(R.id.tvPuppetInfo);
        tvInfo.setText("Whatever you want" );


        // must add this: -otherwise I don't know why, but is not working the click
        RelativeLayout relativeLayout = (RelativeLayout)videoView.getParent();
        relativeLayout.setClickable(true);
        relativeLayout.setOnClickListener(rlClickListener);

        return vi;
    }


    View.OnClickListener rlClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            VideoView videoView = (VideoView)v.findViewById(R.id.videoView);
                videoView.start();

        }
    };
}

row_video.xml

中的代码部分
<?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="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="vertical">

   <!-- Yes, it is in pixel, hardcoded, because this is what is requested -->
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="1280px"
        android:layout_height="720px"
        android:layout_centerHorizontal="true" />


    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/videoView"
        android:layout_below="@+id/videoView"
        android:layout_marginTop="5dp"
        android:focusable="false"
        android:text="25/12/2016 12:59:59"
        android:textAppearance="?android:attr/textAppearanceLarge"
        />


    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/videoView"
        android:layout_below="@+id/videoView"
        android:layout_marginTop="5dp"
        android:drawableRight="@drawable/red_check"
        android:text="whatever you want"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/videoView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="1dp"
        android:gravity="center"
        android:layout_toLeftOf="@+id/tvStatus"
        android:layout_toRightOf="@+id/tvDate"
        android:focusable="false"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>

我希望值得投票! (点击)