- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在创建一个图像编辑应用程序。我在其中提供了打开相机或画廊以选择图片的便利。选择图片后,用户将导航到其他页面。在其他显示图片的页面中,我使用了一个实现了 AppCompactImageView
的 View 。现在,我正在提供使用 edittext 添加文本的便利。单击软键盘的“完成”按钮时,edittext 将消失并被 TextView
取代。现在我想将这个 TextView 拖到整个布局中。但是在 drop 操作之后,它就消失了。下面是我的代码。任何帮助都将不胜感激。
DrawActivity.java
公共(public)类 DrawActivity 扩展 AppCompatActivity {
DrawView imgView;
Button resetBtn, saveBtn;
ImageButton undoBtn;
Bundle extras;
Context context;
EditText addTxtBox;
ImageView brushImg, fontImg;
TextView addedTxtView;
// LinearLayout mainLinear;
RelativeLayout mainRelative;
public int rowX, rowY;
public String txtVal;
private android.widget.RelativeLayout.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_draw);
context = this.getApplicationContext();
mainRelative = (RelativeLayout) findViewById(R.id.relativeLayout1);
imgView = (DrawView) findViewById(R.id.frag_home_iv_main);
resetBtn = (Button) findViewById(R.id.clearBtn);
undoBtn = (ImageButton) findViewById(R.id.undoBtn);
saveBtn = (Button) findViewById(R.id.saveBtn);
addTxtBox = (EditText) findViewById(R.id.addTxt);
brushImg = (ImageView) findViewById(R.id.imageBrush);
fontImg = (ImageView) findViewById(R.id.imageFont);
addedTxtView = (TextView) findViewById(R.id.drawTextView);
addedTxtView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(addedTxtView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("Vishal sdk","in if");
v.startDragAndDrop(dragData, myShadow, null, 0);
} else {
Log.d("Vishal sdk","in else");
v.startDrag(dragData, myShadow, null, 0);
}
return false;
}
});
addedTxtView.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
Log.d("Vishal check", String.valueOf(event.getAction()));
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_STARTED");
return true;
// Do nothing
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
return true;
case DragEvent.ACTION_DRAG_EXITED :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
v.setVisibility(View.VISIBLE);
// view.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
return true;
case DragEvent.ACTION_DRAG_ENDED :
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_ENDED");
return true;
// Do nothing
case DragEvent.ACTION_DROP:
Log.d("Vishal Drag", "ACTION_DROP event");
/* x_cord = (int) event.getX();
y_cord = (int) event.getY();
View view = (View) event.getLocalState();
view.setX(x_cord - (view.getWidth() / 2));
view.setY(y_cord - (view.getWidth() / 2));
view.setVisibility(View.VISIBLE);*/
return true;
// Do nothing
default:
return true;
}
//return true;
}
});
addTxtBox.setImeOptions(EditorInfo.IME_ACTION_DONE);
addTxtBox.setRawInputType(InputType.TYPE_CLASS_TEXT);
addTxtBox.setVisibility(View.GONE);
// addedTxtView.setVisibility(View.GONE);
extras = getIntent().getExtras();
imgView.setImageURI(Uri.parse(extras.getString("selectedImg")));
imgView.setDrawingCacheEnabled(true);
brushImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgView.setBrushActive(true);
}
});
fontImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgView.setBrushActive(false);
addTxtBox.setVisibility(View.VISIBLE);
addTxtBox.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
});
addTxtBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(DrawActivity.this, addTxtBox.getText(), Toast.LENGTH_SHORT).show();
addedTxtView.setVisibility(View.VISIBLE);
txtVal = addTxtBox.getText().toString();
imgView.getTxtValue(addTxtBox.getText().toString());
addedTxtView.setText(addTxtBox.getText().toString());
addedTxtView.setTag(addTxtBox.getText().toString());
addTxtBox.setVisibility(View.GONE);
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(addTxtBox.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
return false;
}
});
resetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgView.resetPaths();
}
});
undoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgView.removeLastPath();
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = imgView.getDrawingCache();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
path.mkdirs();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
File imageFile = new File(path, ts+".png"); // Imagename.png
try{
FileOutputStream out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
try {
out.flush();
out.close();
} catch (IOException e){
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Toast.makeText(context,"Saved", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DrawActivity.this, MainActivity.class);
startActivity(intent);
} catch(FileNotFoundException e) {
Toast.makeText(context,"Error" + e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
public void redrawImage() {
Bitmap bm = imgView.getDrawingCache();
Bitmap proxy = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(proxy);
Paint paint = new Paint();
// paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setTextSize(100);
paint.setAntiAlias(true);
c.drawBitmap(bm, new Matrix(), paint);
c.drawText(txtVal, imgView.getWidth()+rowX, imgView.getHeight()+rowY, paint);
imgView.setImageBitmap(proxy);
}
public void changeColor(View view) {
Log.d("Vishal selected color", view.getTag().toString());
switch (view.getTag().toString()){
case "red":
imgView.setBrushColor(Color.RED);
break;
case "blue":
imgView.setBrushColor(Color.BLUE);
break;
case "green" :
imgView.setBrushColor(Color.GREEN);
break;
case "yellow":
imgView.setBrushColor(Color.YELLOW);
break;
case "purple":
imgView.setBrushColor(getResources().getColor(R.color.purple));
break;
default:
imgView.setBrushColor(Color.BLACK);
}
//
}
activity_draw.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<ImageButton
android:id="@+id/undoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/undo"/>
<Button
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_toLeftOf="@+id/saveBtn"/>
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:orientation="vertical"
android:id="@+id/draw_image_linearView">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.mobilesolution.imgedit.DrawView
android:id="@+id/frag_home_iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/drawTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="100dp"
android:layout_centerInParent="true"
android:textColor="@android:color/black"
android:textSize="16dp"
/>
<EditText
android:id="@+id/addTxt"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:inputType="textMultiLine"
android:maxLines="3"
android:elevation="10dp"/>
</RelativeLayout>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:scrollbars="none"
android:layout_weight="0.2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="447dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/redBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="changeColor"
android:src="@drawable/red_ball"
android:tag="red" />
<ImageView
android:id="@+id/blueBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="changeColor"
android:src="@drawable/blue_ball"
android:tag="blue" />
<ImageView
android:id="@+id/greenBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="changeColor"
android:src="@drawable/green_ball"
android:tag="green" />
<ImageView
android:id="@+id/yellowBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="changeColor"
android:src="@drawable/yellow_ball"
android:tag="yellow" />
<ImageView
android:id="@+id/purpleBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="changeColor"
android:src="@drawable/purple_ball"
android:tag="purple" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/red_ball" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/red_ball" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/red_ball" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/green_ball" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/green_ball" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:id="@+id/brushBtnLayout"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="@android:color/black">
<ImageView
android:id="@+id/imageBrush"
android:layout_width="65dp"
android:layout_height="37dp"
android:layout_marginLeft="66dp"
android:layout_marginStart="66dp"
android:background="@android:color/white"
android:src="@drawable/brush"
android:layout_alignTop="@+id/imageFont"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="@+id/imageFont"
android:layout_width="65dp"
android:layout_height="37dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="52dp"
android:layout_marginRight="52dp"
android:background="@android:color/white"
android:src="@drawable/font" />
</RelativeLayout>
最佳答案
当拖动存在时,您应该设置 X
和 Y
坐标,而不是设置边距。
再次更新 放置 View 是您的 TextView 应该放入的位置。这应该包含拖动监听器
frag_home_iv_main.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
int action = dragEvent.getAction();
View viewdrag = (View) dragEvent.getLocalState();
if(action == DragEvent.ACTION_DRAG_ENTERED){
//do something
}
else if(action == DragEvent.ACTION_DRAG_EXITED){
//do something
Log.d("Vishal Drag", "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
viewdrag .setY(y_cord);
viewdrag .setX(x_cord);
}
else if(action == DragEvent.ACTION_DROP){
//do something
}
return true;
}
});
关于Android-Textview 在拖动操作时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510361/
我在activity_main.xml中有一个Textview设计,我想用它来在另一个textview下面显示一个textview。我从 stackoverlow 本身获取了这段代码,并做了一些更改以
伙计们,我尝试添加另一个 TextView ,但是当我尝试时,新的 TextView 已放置在前一个 TextView 上,如何使第二个 TextView 作为新的第二个 TextView 下降 这是
现在 textview 显示如果我将滚动到底部的语句。看起来 promis textview 变大了。有办法解决吗? 星号下的所有内容都是 promis textview 的一部分。它是一个带有多个换
我有多个 TextView 。所有 TextView 都是一个单词。我想用它们来构建一个段落。但他们不应该失去他们的 TextView 功能。因此它们不应该用作字符串。因为那样的话,我希望它们可以一一
附件是指向我的应用程序屏幕截图的链接和显示我想要实现的功能的视频的链接。基本上我想要的是当我单击任何功能区上的“更多”按钮时,它应该自行展开并且用从 JSON 文件中获取的数据替换文本。功能区的高度应
我想显示 edittext 中的文本,但由于输入了大量字符,布局被破坏了。我如何限制在 textview 上显示的字符数 最佳答案 你可以尝试在 Edittext actionlistener 上使用
是否可以将 textview 环绕在 textview 周围,第二个 textview 将环绕到第一个 textview 下的下一行? 例如: 我尝试过使用 android:layout_weig
假设我有 10 个文本项,所有这些项都对用户可见。每个文本项都有不同的颜色和样式。我不知道实现此目标的最佳方法是什么。 多个静态 TextView - 最容易实现,但性能可能最差。 TextView
我正在尝试创建一个布局,其中有两个 TextViews 彼此相邻。第一个 TextView 具有可变长度并且是单行的。第二个 TextView 包含一个固定标签,应该不被省略。第二个标签必须紧挨着第一
我正在使用 onTouchEvent 方法来缩放 TextView ,但是当我有两个 TextView 并且我只想调整第一个 TextView 而不是第二个 TextView 时,我遇到了麻烦。我的意
在我的 android 应用程序中,我有一个自动完成 TextView 字段,可以向用户建议不同的州名称。代码如下: ArrayAdapter state_adapter=new ArrayAdap
我怎样才能让 textview 开始一个新行,一直从左边开始,同时保留属性“toTheRightOf”username25? 像这样: 最佳答案 我的猜测是 twitch 应
我正在做一个动态 TextView 的例子,它是通过按下一个按钮创建的。它们的创建格式类似于: 1- Textview (fist created) 2- TextView 3- TextView (
我想要 TextView 和描述的自动调整大小的文本 here .我想,“测试”这个词应该在单行上对齐,但它是基于字符包装的(参见另一行的“g”)。我应该如何更改我的配置,以便一切都按我的意愿工作?
我有一个简单的布局,在两个 ImageView 之间包含一个 TextView。 当我在运行时使用“setText()”更改 TextView 时,它发生了变化,但失去了“center_horizo
作为 Android 初学者,我正在尝试显示一棵树。我已经找到了如何以编程方式添加新的 TextView,但我不知道如何放置它们,我的意思是如何根据父节点设置它们的填充/边距。 非常感谢您的回答, 巴
对于 AndroidTV,我创建了一个简单的搜索栏和 TextView 。我正在尝试对齐搜索栏拇指的 TextView 并尝试随着进度移动。 尝试了这些解决方案,但不起作用 link1 , link2
我是 Android Studio 新手,正在编写应用程序。 我正在尝试它,我想知道如何在 Textview1 包含某些内容的情况下启用(例如)Textview2。 我尝试了这段代码,但它不起作用:
我从这里获取 TextView 实例: TextView date = null; try { date = (TextView) getLayoutI
这是在别处被问到的,但解决方案对我不起作用。所以用更多的背景再次摆姿势。问题是一个 Activity 包含一个滚动的音乐标题 TextView ,它被更新的耗时计数器 TextView 中断。 我的
我是一名优秀的程序员,十分优秀!