TShopping

 找回密碼
 註冊
搜索
查看: 1917|回復: 1
打印 上一主題 下一主題

[教學] android指定圖片位置打浮水印

[複製鏈接]
跳轉到指定樓層
1#
發表於 2015-1-28 16:20:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
layout 自行增加editText ImageView button

加入圖片ic_action_search

  1. public class MainActivity extends Activity {
  2.         Bitmap originalBitmap,image;
  3.         ImageView iv_pic;
  4.         EditText et_sample;
  5.         Paint paint;
  6.         @Override
  7.         protected void onCreate(Bundle savedInstanceState) {
  8.                 super.onCreate(savedInstanceState);
  9.                 setContentView(R.layout.activity_main);
  10.                 //image view
  11.                 iv_pic = (ImageView) findViewById(R.id.iv_pic);
  12.                 DisplayMetrics displaymetrics = new DisplayMetrics();
  13.                 getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
  14.                 //dimentions x,y of device to create a scaled bitmap having similar dimentions to screen size
  15.                 int height1 = displaymetrics.heightPixels;
  16.                 int width1 = displaymetrics.widthPixels;
  17.                 //paint object to define paint properties
  18.                 paint = new Paint();
  19.                 paint.setStyle(Paint.Style.FILL);
  20.                 paint.setColor(Color.BLUE);
  21.                 paint.setTextSize(25);
  22.                 //loading bitmap from drawable
  23.                 originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
  24.                 //scaling of bitmap
  25.                 originalBitmap =Bitmap.createScaledBitmap(originalBitmap, width1, height1, false);
  26.                 //creating anoter copy of bitmap to be used for editing
  27.                 et_sample =(EditText)findViewById(R.id.et_txt);
  28. //                        Button btn_save_img = (Button) findViewById(R.id.btn_save_image);
  29.                 Button btn_clr_all = (Button) findViewById(R.id.btn_clr_all);
  30.                 btn_clr_all.setOnClickListener(new OnClickListener() {
  31.                         public void onClick(View v) {
  32.                         //loading original bitmap again (undoing all editing)
  33.                                 image = originalBitmap.copy(Bitmap.Config.RGB_565, true);
  34.                                 iv_pic.setImageBitmap(image);
  35.                                 
  36.                         }
  37.                 });

  38.                 iv_pic.setOnTouchListener(new OnTouchListener() {
  39.                         @Override
  40.                         public boolean onTouch(View arg0, MotionEvent arg1) {
  41.                                 // TODO Auto-generated method stub
  42.                                 String user_text=et_sample.getText().toString();
  43.                                 //gettin x,y cordinates on screen touch
  44.                                 float scr_x=arg1.getRawX();
  45.                                 float scr_y=arg1.getRawY();
  46.                                 //funtion called to perform drawing
  47.                                 createImage(scr_x,scr_y,user_text);
  48.                                 return true;
  49.                         }
  50.                 });
  51.         }
  52.         
  53.         public Bitmap createImage(float scr_x,float scr_y,String user_text){
  54.                 //canvas object with bitmap image as constructor
  55.                 Canvas canvas = new Canvas(image);
  56.                 int viewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
  57.                 //removing title bar hight
  58.                 scr_y=scr_y- viewTop;
  59.                 //fuction to draw text on image. you can try more drawing funtions like oval,point,rect,etc…
  60.                 canvas.drawText(""+user_text, scr_x, scr_y, paint);
  61.                 iv_pic.setImageBitmap(image);
  62.                
  63.                 return image;
  64.         }
複製代碼


這樣就可以了
文章參考:https://xjaphx.wordpress.com/201 ... marking-on-the-fly/

 

臉書網友討論
2#
發表於 2015-7-27 14:00:53 | 只看該作者
谢谢楼主啦~~~~












网游之天谴修罗

版主招募中

*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-25 16:49 , Processed in 0.094049 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表