日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android中拖动文字实现功能,Android:图片中叠加文字,支持拖动改变位置

發布時間:2023/12/10 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中拖动文字实现功能,Android:图片中叠加文字,支持拖动改变位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之所以做了這么一個Demo,是因為最近項目中有一個奇葩的需求:用戶拍攝照片后,分享到微信的同時添加備注,想獲取用戶在微信的彈出框輸入的內容,保存在自己的服務器上。而事實上,這個內容程序是無法獲取的,因此采取了一個折衷方案,將文字直接寫在圖片上。

首先上Demo效果圖:

功能:

1.用戶自由輸入內容,可手動換行,并且行滿也會自動換行。

2.可拖動改變圖片中文本位置(文字不會超出圖片區域)。

3.點擊“生成圖片”按鈕之后,生成一張帶有文字的圖片文件。

代碼不多,直接全部貼上了:

Activity:

/**

* 將文字寫在圖片中(截圖方式),支持拖動文字。

* 說明:很明顯,截圖方式會降低圖片的質量。如果需要保持圖片質量可以使用canvas的方式,將文字直接繪制在圖片之上(不過,使用此方式要實現文字拖動較為復雜)。

*/

public class MainActivity extends AppCompatActivity {

//圖片組件

private ImageView imageView;

//位于圖片中的文本組件

private TextView tvInImage;

//圖片和文本的父組件

private View containerView;

//父組件的尺寸

private float imageWidth, imageHeight, imagePositionX, imagePositionY;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.image_with_text);

imageView = (ImageView) findViewById(R.id.writeText_img);

EditText editText = (EditText) findViewById(R.id.writeText_et);

tvInImage = (TextView) findViewById(R.id.writeText_image_tv);

containerView = findViewById(R.id.writeText_img_rl);

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

imagePositionX = imageView.getX();

imagePositionY = imageView.getY();

imageWidth = imageView.getWidth();

imageHeight = imageView.getHeight();

//設置文本大小

tvInImage.setMaxWidth((int) imageWidth);

}

});

imageView.setImageBitmap(getScaledBitmap(R.mipmap.test_img));

//輸入框

editText.addTextChangedListener(new TextWatcher() {

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

if (s.toString().equals()) {

tvInImage.setVisibility(View.INVISIBLE);

} else {

tvInImage.setVisibility(View.VISIBLE);

tvInImage.setText(s);

}

}

@Override

public void afterTextChanged(Editable s) {

}

});

final GestureDetector gestureDetector = new GestureDetector(this, new SimpleGestureListenerImpl());

//移動

tvInImage.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

gestureDetector.onTouchEvent(event);

return true;

}

});

}

//確認,生成圖片

public void confirm(View view) {

Bitmap bm = loadBitmapFromView(containerView);

String filePath = Environment.getExternalStorageDirectory() + File.separator + image_with_text.jpg;

try {

bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));

Toast.makeText(this, 圖片已保存至:SD卡根目錄/image_with_text.jpg, Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

//以圖片形式獲取View顯示的內容(類似于截圖)

public static Bitmap loadBitmapFromView(View view) {

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

view.draw(canvas);

return bitmap;

}

private int count = 0;

//tvInImage的x方向和y方向移動量

private float mDx, mDy;

//移動

private class SimpleGestureListenerImpl extends GestureDetector.SimpleOnGestureListener {

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

//向右移動時,distanceX為負;向左移動時,distanceX為正

//向下移動時,distanceY為負;向上移動時,distanceY為正

count++;

mDx -= distanceX;

mDy -= distanceY;

//邊界檢查

mDx = calPosition(imagePositionX - tvInImage.getX(), imagePositionX + imageWidth - (tvInImage.getX() + tvInImage.getWidth()), mDx);

mDy = calPosition(imagePositionY - tvInImage.getY(), imagePositionY + imageHeight - (tvInImage.getY() + tvInImage.getHeight()), mDy);

//控制刷新頻率

if (count % 5 == 0) {

tvInImage.setX(tvInImage.getX() + mDx);

tvInImage.setY(tvInImage.getY() + mDy);

}

return true;

}

}

//計算正確的顯示位置(不能超出邊界)

private float calPosition(float min, float max, float current) {

if (current < min) {

return min;

}

if (current > max) {

return max;

}

return current;

}

//獲取壓縮后的bitmap

private Bitmap getScaledBitmap(int resId) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inJustDecodeBounds = true;

BitmapFactory.decodeResource(getResources(), resId, opt);

opt.inSampleSize = Utility.calculateInSampleSize(opt, 600, 800);

opt.inJustDecodeBounds = false;

return BitmapFactory.decodeResource(getResources(), resId, opt);

}

}

一個工具類:

public class Utility {

//計算 inSampleSize 值,壓縮圖片

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;

final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both

// height and width larger than the requested height and width.

while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {

inSampleSize *= 2;

}

}

return inSampleSize;

}

}

相關報道:

在進行數據查詢的時候我們有真分頁和假分頁兩種,所謂真分頁就是按照根據pageIndex(當前頁碼)和pageSize(每頁的記錄條數)去數據庫中查找響應的記錄,而假分 更多

一.基本規則1.函數定義 在python中函數用關鍵字def聲明,參數用逗號隔開,另外需要注意的是函數沒有返回類型.Python函數不指定特定的返回類型,甚至不需要指定是否返回一個.但實際上,每一個python函數都會返回一 個.如果執行了return語句,那么它會返回 更多

總結

以上是生活随笔為你收集整理的android中拖动文字实现功能,Android:图片中叠加文字,支持拖动改变位置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。