|
layout 自行增加editText ImageView button
加入圖片ic_action_search
- public class MainActivity extends Activity {
- Bitmap originalBitmap,image;
- ImageView iv_pic;
- EditText et_sample;
- Paint paint;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //image view
- iv_pic = (ImageView) findViewById(R.id.iv_pic);
- DisplayMetrics displaymetrics = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
- //dimentions x,y of device to create a scaled bitmap having similar dimentions to screen size
- int height1 = displaymetrics.heightPixels;
- int width1 = displaymetrics.widthPixels;
- //paint object to define paint properties
- paint = new Paint();
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.BLUE);
- paint.setTextSize(25);
- //loading bitmap from drawable
- originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
- //scaling of bitmap
- originalBitmap =Bitmap.createScaledBitmap(originalBitmap, width1, height1, false);
- //creating anoter copy of bitmap to be used for editing
- et_sample =(EditText)findViewById(R.id.et_txt);
- // Button btn_save_img = (Button) findViewById(R.id.btn_save_image);
- Button btn_clr_all = (Button) findViewById(R.id.btn_clr_all);
- btn_clr_all.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //loading original bitmap again (undoing all editing)
- image = originalBitmap.copy(Bitmap.Config.RGB_565, true);
- iv_pic.setImageBitmap(image);
-
- }
- });
- iv_pic.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View arg0, MotionEvent arg1) {
- // TODO Auto-generated method stub
- String user_text=et_sample.getText().toString();
- //gettin x,y cordinates on screen touch
- float scr_x=arg1.getRawX();
- float scr_y=arg1.getRawY();
- //funtion called to perform drawing
- createImage(scr_x,scr_y,user_text);
- return true;
- }
- });
- }
-
- public Bitmap createImage(float scr_x,float scr_y,String user_text){
- //canvas object with bitmap image as constructor
- Canvas canvas = new Canvas(image);
- int viewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
- //removing title bar hight
- scr_y=scr_y- viewTop;
- //fuction to draw text on image. you can try more drawing funtions like oval,point,rect,etc…
- canvas.drawText(""+user_text, scr_x, scr_y, paint);
- iv_pic.setImageBitmap(image);
-
- return image;
- }
複製代碼
這樣就可以了
文章參考:https://xjaphx.wordpress.com/201 ... marking-on-the-fly/
|
|