[英]how to share image and textview over it in android
my code share the image only without textView which user put it over image . 我的代码仅共享图像而没有textView,该用户将其放在图像上。 i want to share invitation card via any application (image and text together) . 我想通过任何应用程序共享邀请卡(图像和文本在一起)。 not image only 不只是图像
so my problem is how to share this image which name g7 and textview which have id (tvView ) over image 所以我的问题是如何在图像上共享名称为g7和textview且具有id(tvView)的图像
notice that the user can change the position of textview by setOnTouchListener() method 注意用户可以通过setOnTouchListener()方法更改textview的位置
this is my g7.java code 这是我的g7.java代码
public class g7 extends ActionBarActivity {
Button button;
TextView tvView ;
TextView red ;
TextView green ;
TextView blue ;
TextView yellow ;
TextView purple ;
TextView pink ;
TextView tv ;
float x,y = 0.0f ;
boolean moving = false ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.g7);
tvView = (TextView) findViewById(R.id.tvView) ;
red = (TextView) findViewById(R.id.red) ;
green = (TextView) findViewById(R.id.green) ;
blue = (TextView) findViewById(R.id.blue) ;
yellow = (TextView) findViewById(R.id.yellow) ;
purple = (TextView) findViewById(R.id.purple) ;
pink = (TextView) findViewById(R.id.pink) ;
Intent intent = getIntent();
String texx = intent.getStringExtra("fname") ;
tvView.setText(texx);
tvView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0 , MotionEvent arg1) {
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN :
moving = true ;
break ;
case MotionEvent.ACTION_MOVE :
if (moving)
{
x = arg1.getRawX()- tvView.getWidth()/2 ;
y = arg1.getRawY() - tvView.getHeight() * 3/2 ;
tvView.setX(x) ;
tvView.setY(y) ;
}
break ;
case MotionEvent.ACTION_UP :
moving = false ;
break ;
}
return true;
}
});
red.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Red)) ;
}
return true;
}
});
green.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Green)) ;
}
return true;
}
});
blue.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Blue)) ;
}
return true;
}
});
yellow.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Yellow)) ;
}
return true;
}
});
purple.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Purple)) ;
}
return true;
}
});
pink.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg2 , MotionEvent arg3) {
if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
tvView.setTextColor(getResources().getColor(R.color.Pink)) ;
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.share)
{
PackageManager pm=getPackageManager();
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+"drawable/g7");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share via"));
return true;}
return super.onOptionsItemSelected(item);
}
} }
and this my xml file 这是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id = "@+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".g7" >
<ImageView
android:id="@+id/g7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/g7" />
</LinearLayout >
<LinearLayout
android:id = "@+id/toplinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".g7">
<TextView
android:id="@+id/tvView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text color :"
/>
<TextView
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="red "
android:textColor="@color/Red" />
<TextView
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="green "
android:textColor="@color/Green" />
<TextView
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blue "
android:textColor="@color/Blue"/>
<TextView
android:id="@+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yellow "
android:textColor="@color/Yellow" />
<TextView
android:id="@+id/purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="purple "
android:textColor="@color/Purple"/>
<TextView
android:id="@+id/pink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pink "
android:textColor="@color/Pink" />
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
I assume the RelativeLayout in your xml file represents the whole invitation card, correct? 我假设您的xml文件中的RelativeLayout代表整个邀请卡,对吗? If so: 如果是这样的话:
Basic Strategy: 基本策略:
1) Render the invitation card to a bitmap 1)将邀请卡渲染为位图
2) Share the rendered bitmap 2)分享渲染的位图
For rendering the card , you can use a function something like this. 要渲染卡 ,您可以使用类似以下的功能。 It renders a View onto a Bitmap (the RelativeLayout class is a grandchild of the View class): 它将View呈现到位图上(RelativeLayout类是View类的孙代):
public Bitmap renderViewAsBitmap( View view ) {
view.setDrawingCacheEnabled( true );
view.buildDrawingCache();
return view.getDrawingCache();
}
For saving the Bitmap , you can use a function something like this. 为了保存位图 ,可以使用类似以下的功能。 It outputs a PNG file to the specified path: 它将PNG文件输出到指定的路径:
public boolean saveBitmap( Bitmap bitmap, String filepath ) {
try {
FileOutputStream out = new FileOutputStream( filepath );
bitmap.compress( Bitmap.CompressFormat.PNG, 100, out );
out.flush();
out.close();
} catch( Exception e ) {
Log.e( "g7", "Failed to save bitmap", e );
return false;
}
return true;
}
To generate the URI for sharing , you can use a function something like this. 要生成用于共享的URI ,您可以使用类似以下的功能。
public void shareImageFile( File file ) {
Uri uri = Uri.fromFile( file );
Intent intent = new Intent();
intent.setAction( Intent.ACTION_SEND );
intent.setType( "image/*" );
intent.putExtra( Intent.EXTRA_STREAM, uri );
startActivity( Intent.createChooser( intent, "Share via" ) );
}
You could place the above functions into your g7 class. 您可以将上述函数放入g7类中。 Then when you are ready to share the card, use them to generate a Bitmap, save it to file, and start the sharing intent: 然后,当您准备共享卡时,使用它们生成位图,将其保存到文件中,然后开始共享意图:
Bitmap cardBitmap = renderViewAsBitmap( findViewById( R.id.cardlayout ) );
// Do your own file management. Here I just delete and reuse same file.
String cardFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Android/data/" + getPackageName() + "/";
File cardFolder = new File( cardFolderPath );
cardFolder.mkdirs();
String cardPath = cardFolderPath + "card.png";
File cardFile = new File( cardPath );
if( cardFile.exists() ) {
cardFile.delete();
}
if( saveBitmap( cardBitmap, cardPath ) ) {
shareImageFile( cardFile );
} else {
// TODO: Error handling
}
A couple notes: 几个注意事项:
The above will require your app to have permission to write to external storage: (you could alter it to use internal storage if you don't want that). 以上要求您的应用具有写入外部存储的权限:(如果您不想要,可以将其更改为使用内部存储)。 In AndroidManifest.xml: 在AndroidManifest.xml中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You will also need to give your RelativeLayout an id (I used "@+id/cardlayout" in the above example) 您还需要给您的RelativeLayout一个ID(在上面的示例中,我使用了“ @ + id / cardlayout”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.