0

我正在尝试实现复制笑脸并在消息上显示

  • 但是我该怎么做呢?

我的要求是我必须从我的应用程序中直接打开消息传递/电子邮件默认 android 屏幕,并在网格中显示所有图像和选择的图像附加短信/电子邮件并发送。

  • 如何在 SMS 上从网格和过去复制笑脸并用笑脸图像发送。
  • 如何从网格和过去的电子邮件中复制笑脸并用笑脸图像发送。
  • android gridview中是否可以分页?
4

1 回答 1

0
 - To Load Emoticon provide ArrayList(path of emoticons) to PagerAdpater



    private void loadEmoticons() {

            try {

                ViewPager pager = (ViewPager) findViewById(R.id.emoticons_pager);

                ArrayList<String> paths = new ArrayList<String>();
                if (fileList != null) {
                    for (int j = 0; j < fileList.length; j++) {
                        paths.add(fileList[j]);
                    }

                }

                EmoticonsPagerAdapter adapter = new EmoticonsPagerAdapter(
                        EmailEmoticonsActivity.this, paths, this);
                pager.setAdapter(adapter);

                mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
                mIndicator.setViewPager(pager);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

 1. select emoticon kkeyClickedIndex() it give selected emoticon Index.

       @Override
        public void keyClickedIndex(final int index, final String imagePath) {
            try {

                ImageGetter imageGetter = new ImageGetter() {

                    public Drawable getDrawable(String source) {
                        StringTokenizer st = new StringTokenizer(
                                String.valueOf(index), ".");
                        d = new BitmapDrawable(getResources(),
                                emoticons[Integer.parseInt(st.nextToken())]);
                        d.setBounds(0, 0, d.getIntrinsicWidth(),
                                d.getIntrinsicHeight());

                        return d;
                    }
                };

                Spanned cs = Html.fromHtml("<img src ='" + String.valueOf(index)
                        + "'/>", imageGetter, null);
                int cursorPosition = content.getSelectionStart();

                content.getText().insert(cursorPosition, cs);

            } catch (Exception e) {
                e.getMessage();
            }
        }



 - Grid Adapter : pass image paths (arrayList) base on load all images
   on Grid view, KeyClickListener  interface use to get index of
   click/selected image.



    public class EmoticonsGridAdapter extends BaseAdapter {

        private ArrayList<String> paths;
        private int pageNumber;
        Context mContext;
        String urlImages[];
        KeyClickListener mListener;

        public EmoticonsGridAdapter(Context context, ArrayList<String> paths,
                int pageNumber, KeyClickListener listener) {
            this.mContext = context;
            this.paths = paths;
            this.pageNumber = pageNumber;
            this.mListener = listener;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            View v = convertView;
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.emoticons_item, null);
            }

            final String path = paths.get(position);

            ImageView image = (ImageView) v.findViewById(R.id.item);

            image.setImageBitmap(getImage(path));

            image.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    mListener
                            .keyClickedIndex(
                                    (pageNumber * EmoticonsPagerAdapter.NO_OF_EMOTICONS_PER_PAGE)
                                            + position, path);
                }
            });
            return v;
        }

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

        @Override
        public String getItem(int position) {
            return paths.get(position);
        }

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

        public int getPageNumber() {
            return pageNumber;
        }

        private Bitmap getImage(String path) {
            InputStream in = null;
            try {
            in = new FileInputStream(Constant.DIR + Constant.FILE_NAME
                        + File.separator + path);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Bitmap temp = BitmapFactory.decodeStream(in, null, null);
            return temp;
        }
        public interface KeyClickListener {

        public void keyClickedIndex(int index, String imagePath);
            // public void keyClickedIndex(String indexImage);
        }
    }
 - Pager Emoticon

public class EmoticonsPagerAdapter extends PagerAdapter {

    ArrayList<String> emoticons;
    public static final int NO_OF_EMOTICONS_PER_PAGE = 24;
    FragmentActivity mActivity;
    KeyClickListener mListener;

    public EmoticonsPagerAdapter(FragmentActivity activity,
            ArrayList<String> emoticons, KeyClickListener listener) {
        this.emoticons = emoticons;
        this.mActivity = activity;
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        int nubmer = (int) Math.ceil((double) emoticons.size()
                / (double) NO_OF_EMOTICONS_PER_PAGE);

        return nubmer;

    }

    @Override
    public Object instantiateItem(View collection, int position) {

        View layout = mActivity.getLayoutInflater().inflate(
                R.layout.emoticons_grid, null);

        int initialPosition = position * NO_OF_EMOTICONS_PER_PAGE;
        ArrayList<String> emoticonsInAPage = new ArrayList<String>();

        for (int i = initialPosition; i < initialPosition
                + NO_OF_EMOTICONS_PER_PAGE
                && i < emoticons.size(); i++) {
            emoticonsInAPage.add(emoticons.get(i));
        }

        GridView grid = (GridView) layout.findViewById(R.id.emoticons_grid);
        EmoticonsGridAdapter adapter = new EmoticonsGridAdapter(
                mActivity.getApplicationContext(), emoticonsInAPage, position,
                mListener);
        grid.setAdapter(adapter);

        ((ViewPager) collection).addView(layout);

        return layout;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

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

 - Girdview layout (emoticons_grid.xml) Gridview to load image in pager

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/emoticons_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="5dp"
        android:numColumns="6"
        android:verticalSpacing="8dp" />

 - item.xml signal item of gridview  

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/White" />

 - MainLayout add this to ViewPagerand CirclePageIndicator 

 <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/Gray"
        android:padding="10dip"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        app:pageColor="#808080"
        app:radius="5dp"
        app:strokeColor="#000000"
        app:strokeWidth="2dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/emoticons_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/Gray" />
  • 圆形页面指示器

    公共类 CirclePageIndicator 扩展视图实现 PageIndicator { private static final int INVALID_POINTER = -1;

    private float mRadius;
    private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
    private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
    private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
    private ViewPager mViewPager;
    private ViewPager.OnPageChangeListener mListener;
    private int mCurrentPage;
    private int mSnapPage;
    private float mPageOffset;
    private int mScrollState;
    private int mOrientation;
    private boolean mCentered;
    private boolean mSnap;
    
    private int mTouchSlop;
    private float mLastMotionX = -1;
    private int mActivePointerId = INVALID_POINTER;
    private boolean mIsDragging;
    
    public CirclePageIndicator(Context context) {
        this(context, null);
    }
    
    public CirclePageIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.vpiCirclePageIndicatorStyle);
    }
    
    public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (isInEditMode())
            return;
    
        // Load defaults from resources
        final Resources res = getResources();
        final int defaultPageColor = res
                .getColor(R.color.default_circle_indicator_page_color);
        final int defaultFillColor = res
                .getColor(R.color.default_circle_indicator_fill_color);
        final int defaultOrientation = res
                .getInteger(R.integer.default_circle_indicator_orientation);
        final int defaultStrokeColor = res
                .getColor(R.color.default_circle_indicator_stroke_color);
        final float defaultStrokeWidth = res
                .getDimension(R.dimen.default_circle_indicator_stroke_width);
        final float defaultRadius = res
                .getDimension(R.dimen.default_circle_indicator_radius);
        final boolean defaultCentered = res
                .getBoolean(R.bool.default_circle_indicator_centered);
        final boolean defaultSnap = res
                .getBoolean(R.bool.default_circle_indicator_snap);
    
        // Retrieve styles attributes
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CirclePageIndicator, defStyle, 0);
    
        mCentered = a.getBoolean(R.styleable.CirclePageIndicator_centered,
                defaultCentered);
        mOrientation = a.getInt(
                R.styleable.CirclePageIndicator_android_orientation,
                defaultOrientation);
        mPaintPageFill.setStyle(Style.FILL);
        mPaintPageFill.setColor(a.getColor(
                R.styleable.CirclePageIndicator_pageColor, defaultPageColor));
        mPaintStroke.setStyle(Style.STROKE);
        mPaintStroke.setColor(a
                .getColor(R.styleable.CirclePageIndicator_strokeColor,
                        defaultStrokeColor));
        mPaintStroke.setStrokeWidth(a
                .getDimension(R.styleable.CirclePageIndicator_strokeWidth,
                        defaultStrokeWidth));
        mPaintFill.setStyle(Style.FILL);
        mPaintFill.setColor(a.getColor(
                R.styleable.CirclePageIndicator_fillColor, defaultFillColor));
        mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius,
                defaultRadius);
        mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap);
    
        Drawable background = a
                .getDrawable(R.styleable.CirclePageIndicator_android_background);
        if (background != null) {
            setBackgroundDrawable(background);
        }
    
        a.recycle();
    
        final ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = ViewConfigurationCompat
                .getScaledPagingTouchSlop(configuration);
    }
    
    public void setCentered(boolean centered) {
        mCentered = centered;
        invalidate();
    }
    
    public boolean isCentered() {
        return mCentered;
    }
    
    public void setPageColor(int pageColor) {
        mPaintPageFill.setColor(pageColor);
        invalidate();
    }
    
    public int getPageColor() {
        return mPaintPageFill.getColor();
    }
    
    public void setFillColor(int fillColor) {
        mPaintFill.setColor(fillColor);
        invalidate();
    }
    
    public int getFillColor() {
        return mPaintFill.getColor();
    }
    
    public void setOrientation(int orientation) {
        switch (orientation) {
        case HORIZONTAL:
        case VERTICAL:
            mOrientation = orientation;
            requestLayout();
            break;
    
        default:
            throw new IllegalArgumentException(
                    "Orientation must be either HORIZONTAL or VERTICAL.");
        }
    }
    
    public int getOrientation() {
        return mOrientation;
    }
    
    public void setStrokeColor(int strokeColor) {
        mPaintStroke.setColor(strokeColor);
        invalidate();
    }
    
    public int getStrokeColor() {
        return mPaintStroke.getColor();
    }
    
    public void setStrokeWidth(float strokeWidth) {
        mPaintStroke.setStrokeWidth(strokeWidth);
        invalidate();
    }
    
    public float getStrokeWidth() {
        return mPaintStroke.getStrokeWidth();
    }
    
    public void setRadius(float radius) {
        mRadius = radius;
        invalidate();
    }
    
    public float getRadius() {
        return mRadius;
    }
    
    public void setSnap(boolean snap) {
        mSnap = snap;
        invalidate();
    }
    
    public boolean isSnap() {
        return mSnap;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        if (mViewPager == null) {
            return;
        }
        final int count = mViewPager.getAdapter().getCount();
        if (count == 0) {
            return;
        }
    
        if (mCurrentPage >= count) {
            setCurrentItem(count - 1);
            return;
        }
    
        int longSize;
        int longPaddingBefore;
        int longPaddingAfter;
        int shortPaddingBefore;
        if (mOrientation == HORIZONTAL) {
            longSize = getWidth();
            longPaddingBefore = getPaddingLeft();
            longPaddingAfter = getPaddingRight();
            shortPaddingBefore = getPaddingTop();
        } else {
            longSize = getHeight();
            longPaddingBefore = getPaddingTop();
            longPaddingAfter = getPaddingBottom();
            shortPaddingBefore = getPaddingLeft();
        }
    
        final float threeRadius = mRadius * 3;
        final float shortOffset = shortPaddingBefore + mRadius;
        float longOffset = longPaddingBefore + mRadius;
        if (mCentered) {
            longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f)
                    - ((count * threeRadius) / 2.0f);
        }
    
        float dX;
        float dY;
    
        float pageFillRadius = mRadius;
        if (mPaintStroke.getStrokeWidth() > 0) {
            pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f;
        }
    
        // Draw stroked circles
        for (int iLoop = 0; iLoop < count; iLoop++) {
            float drawLong = longOffset + (iLoop * threeRadius);
            if (mOrientation == HORIZONTAL) {
                dX = drawLong;
                dY = shortOffset;
            } else {
                dX = shortOffset;
                dY = drawLong;
            }
            // Only paint fill if not completely transparent
            if (mPaintPageFill.getAlpha() > 0) {
                canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
            }
    
            // Only paint stroke if a stroke width was non-zero
            if (pageFillRadius != mRadius) {
                canvas.drawCircle(dX, dY, mRadius, mPaintStroke);
            }
        }
    
        // Draw the filled circle according to the current scroll
        float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
        if (!mSnap) {
            cx += mPageOffset * threeRadius;
        }
        if (mOrientation == HORIZONTAL) {
            dX = longOffset + cx;
            dY = shortOffset;
        } else {
            dX = shortOffset;
            dY = longOffset + cx;
        }
        canvas.drawCircle(dX, dY, mRadius, mPaintFill);
    }
    
    public boolean onTouchEvent(android.view.MotionEvent ev) {
        if (super.onTouchEvent(ev)) {
            return true;
        }
        if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
            return false;
        }
    
        final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mLastMotionX = ev.getX();
            break;
    
        case MotionEvent.ACTION_MOVE: {
            final int activePointerIndex = MotionEventCompat.findPointerIndex(
                    ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = x - mLastMotionX;
    
            if (!mIsDragging) {
                if (Math.abs(deltaX) > mTouchSlop) {
                    mIsDragging = true;
                }
            }
    
            if (mIsDragging) {
                mLastMotionX = x;
                if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                    mViewPager.fakeDragBy(deltaX);
                }
            }
    
            break;
        }
    
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (!mIsDragging) {
                final int count = mViewPager.getAdapter().getCount();
                final int width = getWidth();
                final float halfWidth = width / 2f;
                final float sixthWidth = width / 6f;
    
                if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                } else if ((mCurrentPage < count - 1)
                        && (ev.getX() > halfWidth + sixthWidth)) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            }
    
            mIsDragging = false;
            mActivePointerId = INVALID_POINTER;
            if (mViewPager.isFakeDragging())
                mViewPager.endFakeDrag();
            break;
    
        case MotionEventCompat.ACTION_POINTER_DOWN: {
            final int index = MotionEventCompat.getActionIndex(ev);
            mLastMotionX = MotionEventCompat.getX(ev, index);
            mActivePointerId = MotionEventCompat.getPointerId(ev, index);
            break;
        }
    
        case MotionEventCompat.ACTION_POINTER_UP:
            final int pointerIndex = MotionEventCompat.getActionIndex(ev);
            final int pointerId = MotionEventCompat.getPointerId(ev,
                    pointerIndex);
            if (pointerId == mActivePointerId) {
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = MotionEventCompat.getPointerId(ev,
                        newPointerIndex);
            }
            mLastMotionX = MotionEventCompat.getX(ev,
                    MotionEventCompat.findPointerIndex(ev, mActivePointerId));
            break;
        }
    
        return true;
    }
    
    @Override
    public void setViewPager(ViewPager view) {
        if (mViewPager == view) {
            return;
        }
        if (mViewPager != null) {
            mViewPager.setOnPageChangeListener(null);
        }
        if (view.getAdapter() == null) {
            throw new IllegalStateException(
                    "ViewPager does not have adapter instance.");
        }
        mViewPager = view;
        mViewPager.setOnPageChangeListener(this);
        invalidate();
    }
    
    @Override
    public void setViewPager(ViewPager view, int initialPosition) {
        setViewPager(view);
        setCurrentItem(initialPosition);
    }
    
    @Override
    public void setCurrentItem(int item) {
        if (mViewPager == null) {
            throw new IllegalStateException("ViewPager has not been bound.");
        }
        mViewPager.setCurrentItem(item);
        mCurrentPage = item;
        invalidate();
    }
    
    @Override
    public void notifyDataSetChanged() {
        invalidate();
    }
    
    @Override
    public void onPageScrollStateChanged(int state) {
        mScrollState = state;
    
        if (mListener != null) {
            mListener.onPageScrollStateChanged(state);
        }
    }
    
    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        mCurrentPage = position;
        mPageOffset = positionOffset;
        invalidate();
    
        if (mListener != null) {
            mListener.onPageScrolled(position, positionOffset,
                    positionOffsetPixels);
        }
    }
    
    @Override
    public void onPageSelected(int position) {
        if (mSnap || mScrollState == ViewPager.SCROLL_STATE_IDLE) {
            mCurrentPage = position;
            mSnapPage = position;
            invalidate();
        }
    
        if (mListener != null) {
            mListener.onPageSelected(position);
        }
    }
    
    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
        mListener = listener;
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see android.view.View#onMeasure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mOrientation == HORIZONTAL) {
            setMeasuredDimension(measureLong(widthMeasureSpec),
                    measureShort(heightMeasureSpec));
        } else {
            setMeasuredDimension(measureShort(widthMeasureSpec),
                    measureLong(heightMeasureSpec));
        }
    }
    
    /**
     * Determines the width of this view
     * 
     * @param measureSpec
     *            A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureLong(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
    
        if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
            // We were told how big to be
            result = specSize;
        } else {
            // Calculate the width according the views count
            final int count = mViewPager.getAdapter().getCount();
            result = (int) (getPaddingLeft() + getPaddingRight()
                    + (count * 2 * mRadius) + (count - 1) * mRadius + 1);
            // Respect AT_MOST value if that was what is called for by
            // measureSpec
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
    /**
     * Determines the height of this view
     * 
     * @param measureSpec
     *            A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureShort(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
    
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the height
            result = (int) (2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
            // Respect AT_MOST value if that was what is called for by
            // measureSpec
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
    @Override
    public void onRestoreInstanceState(Parcelable state) {
        SavedState savedState = (SavedState) state;
        super.onRestoreInstanceState(savedState.getSuperState());
        mCurrentPage = savedState.currentPage;
        mSnapPage = savedState.currentPage;
        requestLayout();
    }
    
    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState savedState = new SavedState(superState);
        savedState.currentPage = mCurrentPage;
        return savedState;
    }
    
    static class SavedState extends BaseSavedState {
        int currentPage;
    
        public SavedState(Parcelable superState) {
            super(superState);
        }
    
        private SavedState(Parcel in) {
            super(in);
            currentPage = in.readInt();
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(currentPage);
        }
    
        @SuppressWarnings("UnusedDeclaration")
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }
    
            @Override
            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
    

    }

  • 页面指​​示器

    public interface PageIndicator extends ViewPager.OnPageChangeListener { /** * 将指标绑定到 ViewPager。* * @param view */ void setViewPager(ViewPager view);

    /**
     * Bind the indicator to a ViewPager.
     *
     * @param view
     * @param initialPosition
     */
    void setViewPager(ViewPager view, int initialPosition);
    
    /**
     * <p>Set the current page of both the ViewPager and indicator.</p>
     *
     * <p>This <strong>must</strong> be used if you need to set the page before
     * the views are drawn on screen (e.g., default start page).</p>
     *
     * @param item
     */
    void setCurrentItem(int item);
    
    /**
     * Set a page change listener which will receive forwarded events.
     *
     * @param listener
     */
    void setOnPageChangeListener(ViewPager.OnPageChangeListener listener);
    
    /**
     * Notify the indicator that the fragment list has changed.
     */
    void notifyDataSetChanged();
    

    }

于 2013-08-06T05:30:46.500 回答