在android中的图像下方绘制文本

时间:2018-01-25 11:29:26

标签: java android canvas bitmap imageview

我正在尝试在图片下方添加文字,如下所示。

之前的图片:image without text below

之后的图片:image with text below

我可以在图像上绘制文字,但我无法在图像下面绘制它。请帮助我!!

这是我的课程,我尝试在图片下面添加文字:

 public class AddSignature extends AppCompatActivity {

    ImageView image;
    boolean footer = true;
    CheckBox addsign;
    String getName, getJob, getMobile, getEmail;
    Button edit;
    LinearLayout share;
    String url;
    DisplayImageOptions options;
    Toolbar toolbar;

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

        toolbar = (Toolbar) this.findViewById(R.id.toolbar);


        image = (ImageView) findViewById(R.id.imgFullscreen);
        addsign = (CheckBox) findViewById(R.id.c_send_with_sign);
        edit = (Button) findViewById(R.id.edit_user_details);
        share = (LinearLayout) findViewById(R.id.share);

        //universal image loader
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.color.colorPrimary)
                .showImageOnFail(R.color.colorPrimary)
                .resetViewBeforeLoading(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .considerExifParams(true)
                .build();


        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(AddSignature.this, ActivityMyDetails.class);
                startActivity(intent1);
            }
        });


        Intent intent = getIntent();
        // Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
        url = intent.getStringExtra("url");


       /* Glide.with(this).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(image);*/

        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
        ImageLoader.getInstance().displayImage(url, image, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                //spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            }
        });

        SharedPreferences sharedPreferences = AddSignature.this.getSharedPreferences("dgfs", Context.MODE_PRIVATE);
        getName = sharedPreferences.getString("name", "");
        getJob = sharedPreferences.getString("job", "");
        getEmail = sharedPreferences.getString("email", "");
        getMobile = sharedPreferences.getString("mobile", "");


        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addsign.isChecked()) {


                    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
                    Bitmap bm = drawable.getBitmap();


                    Bitmap.Config config = bm.getConfig();
                    int width = bm.getWidth();
                    int height = bm.getHeight();

                    Bitmap newImage = Bitmap.createBitmap(width, height, config);

                    Canvas c = new Canvas(newImage);
                    c.drawBitmap(bm, new Matrix(), null);

                    int i1 = (int) (newImage.getHeight() * 0.15D);

                    Paint paint = new Paint();
                    paint.setColor(Color.RED);
                    paint.setStyle(Paint.Style.FILL);
                    paint.setTextSize(25);
                    c.drawText(getName, 30.0F, (newImage.getHeight()) - i1, paint);
                    c.drawText(getJob, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 20, paint);
                    c.drawText(getEmail, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 5 + i1 / 20 + i1 / 20, paint);
                    c.drawText(getMobile, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 5 + i1 / 5 + i1 / 20 + i1 / 20 + i1 / 20, paint);

                   // image.setImageBitmap(newImage);

                   // BitmapDrawable draw = (BitmapDrawable) image.getDrawable();
                   // Bitmap bitmap = draw.getBitmap();
                    try {
                        FileOutputStream outStream = null;
                        File sdCard = Environment.getExternalStorageDirectory();
                        File dir = new File(sdCard.getAbsolutePath() + "/dgfs");
                        dir.mkdirs();
                        String fileName = String.format("%d.jpg", System.currentTimeMillis());
                        File outFile = new File(dir, fileName);
                        outStream = new FileOutputStream(outFile);
                        newImage.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                        outStream.flush();
                        outStream.close();

                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(outFile));
                        sendBroadcast(intent);

                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("image/jpeg");
                        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + outFile.getAbsolutePath()));
                        startActivity(Intent.createChooser(share, "Share Image"));

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

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

                    }




                  /*  Glide.with(AddSignature.this)
                            .load(url)    // you can pass url too
                            .asBitmap()
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                    // you can do something with loaded bitmap here

                                    // imgView.setImageBitmap(resource);
                                    Bitmap.Config config = resource.getConfig();
                                    int width = resource.getWidth();
                                    int height = resource.getHeight();

                                    Bitmap newImage = Bitmap.createBitmap(width, height, config);

                                    Canvas c = new Canvas(newImage);
                                    c.drawBitmap(resource, new Matrix(), null);

                                    int i1 = (int) (newImage.getHeight() * 0.15D);

                                    Paint paint = new Paint();
                                    paint.setColor(Color.RED);
                                    paint.setStyle(Paint.Style.FILL);
                                    // paint.setTextSize(30);
                                    c.drawText(getName, 30.0F, (newImage.getHeight()) - i1, paint);
                                    c.drawText(getJob, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 20, paint);
                                    c.drawText(getEmail, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 5 + i1 / 20 + i1 / 20, paint);
                                    c.drawText(getMobile, 30.0F, (newImage.getHeight()) - i1 + i1 / 5 + i1 / 5 + i1 / 5 + i1 / 20 + i1 / 20 + i1 / 20, paint);

                                    image.setImageBitmap(newImage);
                                }
                            });*/
                } else {


                    BitmapDrawable draw = (BitmapDrawable) image.getDrawable();
                    Bitmap bitmap = draw.getBitmap();
                    try {
                        FileOutputStream outStream = null;
                        File sdCard = Environment.getExternalStorageDirectory();
                        File dir = new File(sdCard.getAbsolutePath() + "/dgfs");
                        dir.mkdirs();
                        String fileName = String.format("%d.jpg", System.currentTimeMillis());
                        File outFile = new File(dir, fileName);
                        outStream = new FileOutputStream(outFile);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                        outStream.flush();
                        outStream.close();

                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(outFile));
                        sendBroadcast(intent);

                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("image/jpeg");
                        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + outFile.getAbsolutePath()));
                        startActivity(Intent.createChooser(share, "Share Image"));

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

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

                    }

                }

            }
        });



}

请帮助我..

1 个答案:

答案 0 :(得分:0)

您可以使用图片在TextView下方的ImageView中添加文字。例如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5">

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:src="@drawable/your_img"/>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="Your text comes here"/>

</LinearLayout>