在水平列表视图项上动态添加编辑文本

时间:2015-08-01 07:07:07

标签: android android-layout android-activity

我正在开发一个照片编辑器应用程序。我可以选择在图像上添加文字,即写一些CAPTION,或者你可以在该图像上说TAG。我想要的是“A”的点击,编辑文本应添加到图像中。我在哪里可以插入姓名或任何文字。

我想要的是在下面的图片中描述...

enter image description here

我的代码在下面......

Second.java

package com.MyFirstApp.myfirstapp;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Random;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.devsmart.android.ui.HorizontalListView;

public class Second extends Activity {

    TouchImageView img_to_be_zoomedH, img_to_be_zoomed_secondH,
            img_to_be_zoomedV, img_to_be_zoomed_secondV;
    ImageView img_back, img_save;
    HorizontalListView HListView, HListViewFirst, HListViewColor;

    /* Save Parent Layout After Editing */
    RelativeLayout parentLayoutforImgSaving;

    Bitmap bitmap_img, bitmap;

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

        img_to_be_zoomedH = (TouchImageView) findViewById(R.id.img_to_be_zoomedH);
        img_to_be_zoomed_secondH = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondH);
        img_to_be_zoomedV = (TouchImageView) findViewById(R.id.img_to_be_zoomedV);
        img_to_be_zoomed_secondV = (TouchImageView) findViewById(R.id.img_to_be_zoomed_secondV);
        img_back = (ImageView) findViewById(R.id.img_back_icon);
        img_save = (ImageView) findViewById(R.id.img_save_icon);
        HListView = (HorizontalListView) findViewById(R.id.horizontal_list_view);
        HListViewFirst = (HorizontalListView) findViewById(R.id.horizontal_list_view_first);
        HListViewColor = (HorizontalListView) findViewById(R.id.horizontal_list_view_color);
        parentLayoutforImgSaving = (RelativeLayout) findViewById(R.id.imagelayout);

        /* Top Back Icon */
        img_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });

        /* Save Image Icon */
        img_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                saveImgAfterEditing(parentLayoutforImgSaving);
            }
        });

        /* Left Image Touch Event */
        img_to_be_zoomedH
                .setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
                    @Override
                    public void onMove() {
                        img_to_be_zoomed_secondH.setZoom(img_to_be_zoomedH);

                        PointF pointF_img1 = new PointF();
                        pointF_img1 = img_to_be_zoomedH.getScrollPosition();
                        img_to_be_zoomed_secondH.setScrollPosition(
                                1 - pointF_img1.x, pointF_img1.y);
                    }
                });
        /* Right Image touch event */
        img_to_be_zoomed_secondH
                .setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {

                    @Override
                    public void onMove() {
                        img_to_be_zoomedH.setZoom(img_to_be_zoomed_secondH);

                        PointF pointF_img1 = new PointF();
                        pointF_img1 = img_to_be_zoomed_secondH
                                .getScrollPosition();
                        img_to_be_zoomedH.setScrollPosition(1 - pointF_img1.x,
                                pointF_img1.y);
                    }
                });

        /* Top Image Touch Event */
        img_to_be_zoomedV
                .setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {
                    @Override
                    public void onMove() {
                        img_to_be_zoomed_secondV.setZoom(img_to_be_zoomedV);

                        PointF pointF_img1 = new PointF();
                        pointF_img1 = img_to_be_zoomedV.getScrollPosition();
                        img_to_be_zoomed_secondV.setScrollPosition(
                                pointF_img1.x, 1 - pointF_img1.y);
                    }
                });
        /* Bottom Image touch event */
        img_to_be_zoomed_secondV
                .setOnTouchImageViewListener(new com.MyFirstApp.myfirstapp.TouchImageView.OnTouchImageViewListener() {

                    @Override
                    public void onMove() {
                        img_to_be_zoomedV.setZoom(img_to_be_zoomed_secondV);

                        PointF pointF_img1 = new PointF();
                        pointF_img1 = img_to_be_zoomed_secondV
                                .getScrollPosition();
                        img_to_be_zoomedV.setScrollPosition(pointF_img1.x,
                                1 - pointF_img1.y);
                    }
                });

        int[] HorizontalListImages = new int[] { R.drawable.icon_grid,
                R.drawable.icon_text, R.drawable.icon_clip_art };
        final int[] HorizontalListImagesFirst = new int[] {
                R.drawable.icon_go_back, R.drawable.icon_horizontal_grid,
                R.drawable.icon_vertical_grid };

        /* Animation References */
        final Animation slideUp = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.slide_up);
        final Animation slideDown = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.slide_down);

        /* Setting Adapter for Horizontal List Views */
        HorizontalListViewAdapter horizontalListViewAdapter = new HorizontalListViewAdapter(
                Second.this, HorizontalListImages);
        HListView.setAdapter(horizontalListViewAdapter);

        /* Horizontal List View Item Click Listener */
        HListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                switch (position) {
                case 0:
                    HorizontalListViewAdapterFirst horizontalListViewAdapterFirst = new HorizontalListViewAdapterFirst(
                            Second.this, HorizontalListImagesFirst);
                    HListViewFirst.setAdapter(horizontalListViewAdapterFirst);

                    HListView.startAnimation(slideDown);
                    HListView.setVisibility(View.GONE);
                    HListViewFirst.startAnimation(slideUp);
                    HListViewFirst.setVisibility(View.VISIBLE);
                    break;

                case 1:
                    /* Space for adding dynamic Edit-Text */
                    break;
                default:
                    break;
                }
            }
        });

        HListViewFirst.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                /* HighLight Selected Option */
                for (int i = 0; i < HListViewFirst.getChildCount(); i++) {
                    if (position == i) {
                        HListViewFirst.getChildAt(i).setBackgroundColor(
                                Color.GRAY);
                    } else {
                        HListViewFirst.getChildAt(i).setBackgroundColor(
                                Color.TRANSPARENT);
                    }
                }

                /* Get Device`s Height, Width */
                DisplayMetrics displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(
                        displaymetrics);
                int height = displaymetrics.heightPixels;
                int width = displaymetrics.widthPixels;

                switch (position) {
                case 0:
                    HListViewFirst.startAnimation(slideDown);
                    HListViewFirst.setVisibility(View.GONE);
                    HListView.startAnimation(slideUp);
                    HListView.setVisibility(View.VISIBLE);
                    break;

                case 1:
                    /* Setting Center Layout For Both Images */
                    RelativeLayout.LayoutParams ImagelayoutParams1 = new RelativeLayout.LayoutParams(
                            width, width);
                    ImagelayoutParams1.addRule(RelativeLayout.CENTER_IN_PARENT);
                    ((RelativeLayout) findViewById(R.id.imagelayout))
                            .setLayoutParams(ImagelayoutParams1);

                    /* Setting Left ImageView width, height */
                    RelativeLayout.LayoutParams layoutParamsLeft = new RelativeLayout.LayoutParams(
                            width / 2, width);
                    layoutParamsLeft.setMargins(0, 0, 0, 0);
                    img_to_be_zoomedH.setLayoutParams(layoutParamsLeft);
                    img_to_be_zoomedH.setVisibility(view.VISIBLE);

                    bitmap_img = ((BitmapDrawable) img_to_be_zoomedH
                            .getDrawable()).getBitmap();
                    bitmap_img = flipImage(bitmap_img, 2);
                    img_to_be_zoomed_secondH.setImageBitmap(bitmap_img);

                    /* Setting Right ImageView width, height */
                    RelativeLayout.LayoutParams layoutParamsRight = new RelativeLayout.LayoutParams(
                            width / 2, width);
                    layoutParamsRight.setMargins(width / 2, 0, 0, 0);
                    img_to_be_zoomed_secondH.setLayoutParams(layoutParamsRight);
                    // img_to_be_zoomedH.invalidate();
                    img_to_be_zoomed_secondH.setVisibility(view.VISIBLE);

                    /* Hiding Vertical TouchIMageViews */
                    if ((img_to_be_zoomedV.getVisibility() == view.VISIBLE)
                            || (img_to_be_zoomed_secondV.getVisibility() == view.VISIBLE)) {
                        img_to_be_zoomedV.setVisibility(view.GONE);
                        img_to_be_zoomed_secondV.setVisibility(view.GONE);
                    }
                    break;

                case 2:
                    /* Setting Center Layout For Both Images */
                    RelativeLayout.LayoutParams ImagelayoutParams2 = new RelativeLayout.LayoutParams(
                            width, width);
                    ImagelayoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
                    ((RelativeLayout) findViewById(R.id.imagelayout))
                            .setLayoutParams(ImagelayoutParams2);

                    /* Setting Top ImageView width, height */
                    RelativeLayout.LayoutParams layoutParamsTop = new RelativeLayout.LayoutParams(
                            width, width / 2);
                    layoutParamsTop.setMargins(0, 0, 0, 0);
                    img_to_be_zoomedV.setLayoutParams(layoutParamsTop);
                    img_to_be_zoomedV.setVisibility(view.VISIBLE);

                    bitmap_img = ((BitmapDrawable) img_to_be_zoomedV
                            .getDrawable()).getBitmap();
                    bitmap_img = flipImage(bitmap_img, 1);
                    img_to_be_zoomed_secondV.setImageBitmap(bitmap_img);

                    /* Setting Bottom ImageView width, height */
                    RelativeLayout.LayoutParams layoutParamsBottom = new RelativeLayout.LayoutParams(
                            width, width / 2);
                    layoutParamsBottom.setMargins(0, width / 2, 0, 0);
                    img_to_be_zoomed_secondV
                            .setLayoutParams(layoutParamsBottom);
                    // img_to_be_zoomedV.invalidate();
                    img_to_be_zoomed_secondV.setVisibility(view.VISIBLE);

                    /* Hiding Horizontal TouchIMageViews */
                    if ((img_to_be_zoomedH.getVisibility() == view.VISIBLE)
                            || (img_to_be_zoomed_secondH.getVisibility() == view.VISIBLE)) {
                        img_to_be_zoomedH.setVisibility(view.GONE);
                        img_to_be_zoomed_secondH.setVisibility(view.GONE);
                    }
                    break;

                default:
                    break;
                }
            }
        });

        /* Getting ImageURI from Gallery from Main Activity */
        Uri selectedImgUri = getIntent().getData();
        if (selectedImgUri != null) {
            String[] selectedImgPath = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImgUri,
                    selectedImgPath, null, null, null);
            cursor.moveToFirst();

            int indexCol = cursor.getColumnIndex(selectedImgPath[0]);
            String imgPath = cursor.getString(indexCol);
            cursor.close();
            img_to_be_zoomedH.setImageBitmap(BitmapFactory.decodeFile(imgPath));
            img_to_be_zoomedV.setImageBitmap(BitmapFactory.decodeFile(imgPath));
        }

        /* Getting ImageBitmap from Camera from Main Activity */
        Intent intent_camera = getIntent();
        Bitmap camera_img_bitmap = (Bitmap) intent_camera
                .getParcelableExtra("BitmapImage");
        if (camera_img_bitmap != null) {
            img_to_be_zoomedH.setImageBitmap(camera_img_bitmap);
            img_to_be_zoomedV.setImageBitmap(camera_img_bitmap);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /* Flip Image Function */
    public Bitmap flipImage(Bitmap src, int type) {
        Matrix matrix = new Matrix();
        /* Flip vertically */
        if (type == 1) {
            matrix.preScale(1.0f, -1.0f);
            /* Flip horizontally */
        } else if (type == 2) {
            matrix.preScale(-1.0f, 1.0f);
        } else {
            return null;
        }
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
                matrix, true);
    }

    /* Save Image Function */
    private void saveImgAfterEditing(RelativeLayout perent) {
        try {
            View content = parentLayoutforImgSaving;
            content.setDrawingCacheEnabled(true);
            content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap bitmap = content.getDrawingCache();

            String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/" + getString(R.string.app_name));
            if (!mFolder.exists()) {
                mFolder.mkdir();
            }
            Calendar c = Calendar.getInstance();
            String s = getString(R.string.app_name) + c.getTimeInMillis()
                    + ".png";

            File f = new File(mFolder.getAbsolutePath(), s);

            FileOutputStream fos = null;
            fos = new FileOutputStream(f);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            bitmap.recycle();
            Toast.makeText(getBaseContext(), "Image Saved", 5000).show();
            addImageGallery(f);
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), "Failed To Save", 5000).show();
            e.printStackTrace();
        }
    }

    /* Save Image to Direct Gallery, No Need to Refresh */
    private void addImageGallery(File file) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
        getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }
}

activity_second.java

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.MyFirstApp.myfirstapp.Second" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp" >

        <ImageView
            android:id="@+id/img_back_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:contentDescription="@string/img_back_icon"
            android:src="@drawable/icon_back" />

        <TextView
            android:id="@+id/txtview_app_name_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/txtview_app_name_top" />

        <ImageView
            android:id="@+id/img_save_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/img_save_icon"
            android:src="@drawable/icon_save" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/imagelayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <com.MyFirstApp.myfirstapp.TouchImageView
            android:id="@+id/img_to_be_zoomedH"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="23dp"
            android:src="@drawable/home" >
        </com.MyFirstApp.myfirstapp.TouchImageView>

        <com.MyFirstApp.myfirstapp.TouchImageView
            android:id="@+id/img_to_be_zoomed_secondH"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" >
        </com.MyFirstApp.myfirstapp.TouchImageView>

        <com.MyFirstApp.myfirstapp.TouchImageView
            android:id="@+id/img_to_be_zoomedV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" >
        </com.MyFirstApp.myfirstapp.TouchImageView>

        <com.MyFirstApp.myfirstapp.TouchImageView
            android:id="@+id/img_to_be_zoomed_secondV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" >
        </com.MyFirstApp.myfirstapp.TouchImageView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.devsmart.android.ui.HorizontalListView
            android:id="@+id/horizontal_list_view"
            android:layout_width="wrap_content"
            android:layout_height="40dp" >
        </com.devsmart.android.ui.HorizontalListView>

        <com.devsmart.android.ui.HorizontalListView
            android:id="@+id/horizontal_list_view_first"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:choiceMode="singleChoice"
            android:listSelector="@drawable/horizontal_list_view_selector"
            android:visibility="gone" >
        </com.devsmart.android.ui.HorizontalListView>

        <com.devsmart.android.ui.HorizontalListView
            android:id="@+id/horizontal_list_view_color"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:visibility="gone" >
        </com.devsmart.android.ui.HorizontalListView>
    </RelativeLayout>

</RelativeLayout>

我想要类似下图...

enter image description here

提前致谢。

0 个答案:

没有答案