2016-03-24 4 views
0

enter image description hereКак сделать круговое отверстие, и это круговое отверстие должно появиться сверху пейджера с изображениями?

Это изображение выше полно, как прямоугольник. Ниже приведен исходный код программы, которую я пробовал. Программа при выполнении отображает изображения без проблем.

Но мне нужна круглая маска на вид. Нравится это.

enter image description here

**activity_main** 

<?xml version="1.0" encoding="utf-8"?> 
<fragment 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:id="@+id/fragment" 
android:name="com.example.shrishyl.circleview.MainActivityFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:layout="@layout/fragment_main" /> 

fragment_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:gravity="center_horizontal" 
android:layout_width="fill_parent" android:layout_height="fill_parent"> 
<android.support.v4.view.ViewPager 
android:id="@+id/pager" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</android.support.v4.view.ViewPager> 
</LinearLayout> 

swipe_fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/black" > 
<ImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/imageView" 
android:layout_gravity="center_horizontal" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="50dp"/> 
</RelativeLayout> 

MainActivit у

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.BitmapShader; 
import android.graphics.Paint; 
import android.graphics.Shader; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.graphics.drawable.RoundedBitmapDrawable; 
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
public class MainActivity extends FragmentActivity 
{ 
    static final int NUM_ITEMS = 4; 
    ImageFragmentPagerAdapter imageFragmentPagerAdapter; 
    ViewPager viewPager; 
    public static final String[] IMAGE_NAME = {"antartica", "croatia", "iceland", "india"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager()); 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    viewPager.setAdapter(imageFragmentPagerAdapter); 
    } 

    public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter 
    { 
     public ImageFragmentPagerAdapter(FragmentManager fm) { super(fm); } 

     @Override 
     public int getCount() { return NUM_ITEMS; } 

     @Override 
     public Fragment getItem(int position) 
     { 
     SwipeFragment fragment = new SwipeFragment(); 
     return fragment.newInstance(position); 
     } 
    } 

    public static class SwipeFragment extends Fragment 
    { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
     { 
      View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false); 
      ImageView imageView = (ImageView) swipeView.findViewById(R.id.imageView); 
      Bundle bundle = getArguments(); 
      int position = bundle.getInt("position"); 
      String imageFileName = IMAGE_NAME[position]; 
      int imgResId = getResources().getIdentifier(imageFileName, "drawable", "com.example.shrishyl.circleview"); 
      Bitmap imageBitmap= BitmapFactory.decodeResource(getResources(), imgResId); 
      imageView.setImageBitmap(imageBitmap); 
      return swipeView; 
     } 
     static SwipeFragment newInstance(int position) 
     { 
      SwipeFragment swipeFragment = new SwipeFragment(); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("position", position); 
      swipeFragment.setArguments(bundle); 
      return swipeFragment; 
     } 
    } 
} 

ответ

1

Вы можете сделать это с помощью холста, как показано ниже:

public static class SwipeFragment extends Fragment 
     { 
      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
      { 
       View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false); 
       ImageView imageView = (ImageView) swipeView.findViewById(R.id.imageView); 
       Bundle bundle = getArguments(); 
       int position = bundle.getInt("position"); 
       String imageFileName = IMAGE_NAME[position]; 
       int imgResId = getResources().getIdentifier(imageFileName, "drawable", "com.example.shrishyl.circleview"); 
       Bitmap imageBitmap= BitmapFactory.decodeResource(getResources(), imgResId); 
       Bitmap circleImage= CircularImage.addPicture(imageBitmap);//By calling this method your image will be converted a circular image 
imageView.setImageBitmap(circleImage);//Here you will set the imageview to the Circular image which was returned by the addPicture Function() in CircularImage class 

       return swipeView; 
      } 
      static SwipeFragment newInstance(int position) 
      { 
       SwipeFragment swipeFragment = new SwipeFragment(); 
       Bundle bundle = new Bundle(); 
       bundle.putInt("position", position); 
       swipeFragment.setArguments(bundle); 
       return swipeFragment; 
      } 
     } 



    } 
     public class CircularImage{ 
     public static Bitmap addPicture(Bitmap bitmap){ 
      Bitmap result = null; 
      try { 
       result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); 
       Canvas canvas = new Canvas(result);// Canvas is class used for drawing , here you will be writing into the result Bitmap 

       int color = 0xff424242; 
       Paint paint = new Paint(); 
       Rect rect = new Rect(0, 0, 200, 200); 

       paint.setAntiAlias(true); 
       canvas.drawARGB(0, 0, 0, 0); 
       paint.setColor(color); 
       canvas.drawCircle(50, 50, 50, paint);//This draws a circle 
       paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
       canvas.drawBitmap(bitmap, rect, rect, paint);// this puts the image into the circle 
      } catch (NullPointerException e) { 
      } catch (OutOfMemoryError o) { 
      } 
      return result; 
     } 
} 

Что вы здесь делаете просто вы создаете Bitmap intially и рисуя круг внутри него. Позже вы рисуете растровое изображение внутри круга. Поэтому в вашем случае просто вызовите эту функцию и передайте битмап, который вы хотите добавить в отверстие круга в качестве параметра. Это должно сработать для вас. ThankYou

+0

вы измените код выше .. я не получаю вам .. извините – shrishyl47

+0

уверен, дайте мне минуту – Jois

+0

Спасибо за ваш код .. Но круг движется вместе с видом салфетки .. Я хочу это отверстие круга должно быть зафиксировано на одном виде, а изображения должны быть скошены всем внутри круга. – shrishyl47