2016-02-18 104 views
0

我想點擊一個圖像,並設置VideoView的可見性,並使imageView不可見。但是,當我點擊圖像白色nbackground出現。請幫助。 Android VideoView與ImageView

feedImageView.setOnClickListener(new OnClickListener() { 
    String substr2 =item.getImge().substring(item.getImge().length() - 3); 
    @Override 
    public void onClick(View v) { 
     if(item.getImge()!=null) 
     { 
      if(substr2.equals("jpg")) 
      { 
       Log.e(TAG,"Its image"); 
      } 
      else if(substr2.equals("mp4")) 
      { 
       feedImageView.setVisibility(View.INVISIBLE); 
       MediaController mc=new MediaController(getActivity()); 
       mc.setAnchorView(vid); 
       mc.setMediaPlayer(vid); 
       Log.e(TAG,"In mp4 "+item.getImge()); 
       Uri uri = Uri.parse(item.getImge()); 
       vid.setMediaController(mc); 
       vid.setVideoURI(uri); 
       vid.setVisibility(View.VISIBLE); 
       vid.start(); 

       Log.e(TAG, "Its video"); 
      } 
     } 
    } 
}); 

佈局中的ImageView和videoView

<com.example.adapter.FeedImageView 
    android:id="@+id/feedImage1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" 
    android:layout_marginTop="10dp" 
    android:scaleType="fitXY" 
    android:visibility="visible" /> 
<VideoView 
    android:id="@+id/vid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:scaleType="fitXY" 
    android:visibility="invisible" /> 

完整的佈局文件 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/feed_bg" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <com.android.volley.toolbox.NetworkImageView 
       android:id="@+id/profilePic" 
       android:layout_width="@dimen/feed_item_profile_pic" 
       android:layout_height="@dimen/feed_item_profile_pic" 
       android:scaleType="fitCenter" > 
      </com.android.volley.toolbox.NetworkImageView> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 
       <TextView 
        android:id="@+id/name" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textSize="@dimen/feed_item_profile_name" 
        android:textColor="#0f84cc" 
        android:textStyle="bold" /> 
      </LinearLayout> 
     </LinearLayout> 

     <!-- <VideoView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:layout_marginTop="15dp" 
      android:background="#000000" 
      /> --> 

      <com.example.adapter.FeedImageView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:layout_marginTop="10dp" 
      android:scaleType="fitXY" 
      android:visibility="visible" /> 
     <VideoView 
      android:id="@+id/vid" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      /> 

     <TextView 
      android:id="@+id/txtStatusMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:paddingTop="@dimen/feed_item_status_pad_top" /> 

     <Button 
      android:id="@+id/downloadbtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Download" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:padding="10dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="10dp" 
      android:background="#0f84cc" 
      /> 
    </LinearLayout> 
</LinearLayout> 
+0

logcat中的任何異常?你是否100%確定你使用的URI是正確的? –

+0

@KirillK使用Logcat獲取uri及其mp4格式 - URI是https://scontent.cdninstagram.com/t50.2886-16/12708125_217442355268913_651092529_n.mp4 – zek54

+0

好的,那麼請發佈完整的佈局文件 –

回答

0

而是採用隱形使用了:

替換:

feedImageView.setVisibility(View.INVISIBLE); 

有:

feedImageView.setVisibility(View.GONE); 
+0

使用你的方法,ImageView會崩潰,但VideoView不會出現。 – zek54

+0

請發佈logcat? –

0

爲了讓圖像和視頻堆疊在另一個之上,你應該把他們在這樣的容器:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<com.example.adapter.FeedImageView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:layout_marginTop="10dp" 
      android:scaleType="fitXY" 
      android:visibility="visible" /> 
     <VideoView 
      android:id="@+id/vid" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp" 
      android:visibility="invisible" 
      /> 
</FrameLayout> 

注意,視頻的高度match_parent使其與初始圖像的尺寸相同

+0

我已經遵循了你的解決方案,但VideoView在ImageView崩潰後不會變得可見。 – zek54

+0

你聽到視頻播放的聲音嗎?您是否要求清單中的互聯網許可,以便視頻可以下載? –

+0

我擁有所有必要的權限,我實際上製作了一個Instagram項目,並從Feed中獲取圖片,但無法將視頻放入已定義的圖像中,因此想要在單擊imageview時獲得videoView.Also我也聽到點擊時沒有聲音。 – zek54