如何将矩形边框添加到视图寻呼机的当前视图

时间:2015-11-26 09:54:34

标签: android android-viewpager image-gallery android-pageradapter

在底部视图寻呼机中,如何将矩形边框添加到仅选定的页面,如图库

我尝试过以下代码

viewpagerFragment.class

 private void loadViewPager() {
        String subAlbum = Constants.VEHICLE_NAME + "" + Constants.VEHICLE_REG_NO;
        final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
        final String orderBy = MediaStore.Images.Media._ID + " DESC";
        Cursor imagecursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns,
                MediaStore.Images.Media.DATA + " like ? ",
                new String[]{"%/ADC/" + subAlbum + "%"},
                orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        this.thumbnails = new Bitmap[this.count];
        this.arrPath = new String[this.count];

        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                    context.getApplicationContext().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MINI_KIND, null);
            arrPath[i] = imagecursor.getString(dataColumnIndex);
        }

        bitmaps = new ArrayList<Bitmap>(thumbnails.length);
        for (int j = 0; j < thumbnails.length; j++) {
            bitmaps.add(thumbnails[j]);
        }

        galImageAdapter = new ViewPagerAdapter(mainActivity, bitmaps);
        viewPager.setAdapter(galImageAdapter);

        viewPager.setPageMargin(
                getResources().getDimensionPixelOffset(R.dimen.viewpager_margin));

        //A little space between pages
        //If hardware acceleration is enabled, you should also remove
        // clipping on the pager for its children.
        viewPager.setClipChildren(false);
        viewPager.setClipToPadding(false);
        viewPager.setPadding(10, 0, 10, 0);

        viewPager.setOffscreenPageLimit(3);



        Log.d(Constants.LOG_TAG,"Constants.SELECTED_PHOTO"+Constants.SELECTED_PHOTO);
        viewPager.setCurrentItem(Constants.SELECTED_PHOTO);

        imagecursor.close();
    }


viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                imagePath = arrPath[position];
                selectedPosition = position;
                setSelectedImage(selectedPosition);
                View view = ((ViewGroup)viewPager.findViewById(R.id.home_container)).getChildAt(position);

                if(viewPager.getCurrentItem()==position) {
                    view.setBackground(context.getResources().getDrawable(R.drawable.imageview_rectangle_selection));
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                View view =((ViewGroup)viewPager.findViewById(R.id.home_container)).getChildAt(position);
                Log.d(Constants.LOG_TAG, "view" + view);
                if(viewPager.getCurrentItem()==position) {
                    view.setBackground(context.getResources().getDrawable(R.drawable.imageview_rectangle_selection));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });

viewPager适配器

public class ViewPagerAdapter extends PagerAdapter {

    private Activity context=null;
    private View mCurrentView;

    //ImageView imageView;

    private List<Bitmap> plotsImages;

    private static ViewHolder holder;
    ImageView imageView;
    public static int flag=0;

    public ViewPagerAdapter(Activity context, List<Bitmap> plotsImages) {

        this.context = context;
        this.plotsImages = plotsImages;
    }

    public int getCount() {
        return plotsImages.size();
    }

    private static class ViewHolder {
        ImageView imageView;
    }
    public void removeitem(int position){
        plotsImages.remove(position);
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        final int pos=position;
        container.setId(R.id.home_container);
        View page=
                context.getLayoutInflater().inflate(R.layout.view_pager_page_layout, container, false);
        imageView=(ImageView)page.findViewById(R.id.imageView);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageBitmap(plotsImages.get(position));
        container.addView(page);

        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                flag = 1;
                PhotoGalleryFragment.setSelectedImage(pos);
                PhotoGalleryFragment.imagePath = PhotoGalleryFragment.arrPath[pos];
            }
        });
        return page;
    }

    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return(view == object);
    }


    @Override
    public float getPageWidth(int position) {
        return (0.17f);
    }
}

viewpagerFragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    tools:context=".MainActivity">




    <app.adc.com.utils.GalleryViewPager
        android:id="@+id/view_pager"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/transparent"
        android:layout_marginBottom="10dp">

    </app.adc.com.utils.GalleryViewPager>



</RelativeLayout>

例如

我想高亮显示所选页面,如下图所示。 (选择鼠​​标)

enter image description here

1 个答案:

答案 0 :(得分:0)

这样的事情?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class ScoreDisplay : MonoBehaviour {

public  Image strick;
public Text[] rollTexts, frameTexts;



public void FillRolls (List<int> rolls) {
    string scoresString = FormatRolls (rolls);
    for (int i = 0; i < scoresString.Length; i++) {
        rollTexts[i].text = scoresString[i].ToString();
    }
}

public void FillFrames (List<int> frames) {
    for (int i = 0; i < frames.Count; i++) {
        frameTexts[i].text = frames[i].ToString();
    }
}

public static string FormatRolls (List<int> rolls) {
    string output = "";

    for (int i = 0; i < rolls.Count; i++) {
        int box = output.Length + 1;                            // Score box 1 to 21 

        if (rolls[i] == 0) {                                    // Always enter 0 as -
            output += "-";
            strick.enabled = true;
            Debug.LogError("Better Luck Next Time");
        } else if (box % 2 == 0 && rolls[i-1]+rolls[i] == 10) { // SPARE anywhere
            output += "/";  
            Debug.LogError("Its Spare");
        } else if (box >= 19 && rolls[i] == 10) {               // STRIKE in frame 10
            output += "X";
            Debug.LogError("Congo you got Strick");
        } else if (rolls[i] == 10) {                            // STRIKE in frame 1-9
            output += "X ";
            Debug.LogError("Its Strick");
        } else {
            output += rolls[i].ToString();                      // Normal 1-9 bowl
            Debug.LogError("Try Your Hard");
        }
    }

    return output;
}

}