I'm using Matrix and Bitmap for imageView,
I want to setup position and Scaling for image as imageView.
(imageView is always showed x = 0, y = 0 )
Scalling is worked , but translate position is not working.
Please help me regarding this.
here is my code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_entrance, container);
ImageButton setting_button = (ImageButton) root.findViewById(R.id.settings_next);
setting_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("next_page", false);
startActivity(intent);
}
});
final ImageView backGroundImageView = (ImageView) root.findViewById(R.id.fragment_entrance_background_imageview);
mLogoImageView = (ImageView) root.findViewById(R.id.fragment_entrance_logo_imageview);
Matrix mMatrix = new Matrix();
mMatrix.reset();
mMatrix.postTranslate(mCenterX - mImgWidth*0.5f, mCenterY - mImgHeight * 0.5f);
mMatrix.postScale(mScaleX, mScaleY, mCenterX, mCenterY);
mLogoImageView.invalidate();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mLogoImgId);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) mImgWidth, (int) mImgHeight, mMatrix, true);
mLogoImageView.setImageBitmap(newBitmap);
return root;
}
and here is xml file.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/entrance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.receptionist.receptionist.Entrance.EntranceFragment">
<ImageView
android:id="#+id/fragment_entrance_background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="0dp">
<ImageView
android:id="#+id/fragment_entrance_logo_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"/>
</RelativeLayout>
<ImageView
android:id="#+id/dummy_button"
android:layout_width="111dp"
android:layout_height="94dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="40dp"
android:background="#color/black_overlay"
android:src="#drawable/button_next" />
<ImageButton
android:id="#+id/settings_next"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="right|top"
android:background="#color/black_overlay"
android:src="#drawable/setting_w" />
</FrameLayout>
Related
I have a Button and a ImageView on the same "Page". Now, I made a OnClickListener and when I Click the Button the Page clears and my Drawing appears. I think I have to change the setContentView(meinView) but I don't know how ?
I want when I click the Button that the Drawing will be made in the ImageView on the same "Page". I'm new in android-programming and I'm not sure that the xml-File is correct. It was made from the Android Studio 3.5.3.
The XML-File:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="422dp"
android:layout_height="452dp"
android:layout_marginBottom="296dp"
android:background="#555555"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="253dp"
android:layout_marginTop="166dp"
android:onClick="BeimKlick"
android:text="Klick mich" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="6dp"
android:elevation="2dp"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="514dp" />
</RelativeLayout>
My Main-Activity:
MeinView meinView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
meinView = new MeinView(this);
meinView.setBackgroundColor(Color.DKGRAY);
setContentView(R.layout.activity_main);
final Button button1 = findViewById(R.id.button1);
final ProgressBar progressbar1 = findViewById(R.id.progressBar1);
final ImageView iView = findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(meinView);
//setContentView(R.id.imageView);
}
});
}
Here is my Drawing-Class:
int x;
for (x=0;x<=200;x+=5)
{
canvas.drawLine(x, 0, x, 1000, paint);
}
int y;
for (y=0;y<=200;y+=5)
{
canvas.drawLine(0, y, 1000, y, paint);
}
canvas.scale(6, 6);
paint.setColor(Color.RED);
canvas.drawText("Test", 20, 50, paint);
canvas.drawText("Row1", 20, 70, paint);
canvas.drawText("Row2", 20, 90, paint);
OK I used Framelayout instead of Relativelayout, but the same result as before. The controls (buttons/progressbar etc.) disappear and the drawing can be seen. Also I changed ImageView in SurfaceView but the same result (because I think ImageView is maybe not up-to-date anymore)
Use FrameLayout for overlapping in your XML instead of RelativeLayout.
I need to set the images to grid view dynamically. but i'm getting exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView
I need to get the view of the grid without onclick. So please help me.
private void setTickMark(String icon_name_val) {
ImageView imageView = (ImageView) rootView;
//getting clicked image
String iconimg = icon_name_val;
String iconimgname = iconimg;
// int idval = getResources().getIdentifier(iconimgname, "drawable",getActivity().getPackageName());
// Drawable drawable = getResources().getDrawable(idval);
// Log.d("dra",String.valueOf(drawable));
int resID = getResources().getIdentifier(iconimgname, "drawable", getActivity().getPackageName());
Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
// Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
c.drawBitmap(resized, 0, 0, null);
c.drawBitmap(bitmap2, 0, 0, null);
imageView.setImageBitmap(resultBitmap);
}
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/popup"
android:layout_height="0dp"
android:layout_weight=".90"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_marginTop="20dp">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="4dp"
android:duplicateParentState="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"/>
</LinearLayout>
</LinearLayout>
Add an imageview to your gridview
then only define ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView1);
I am trying to create a PhotoFrame app, for that i have a frame and i add an image to that frame from gallery.This Frame is in one of the layout in the main layout am trying to take screen shoot for that layout.
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root"
tools:context="com.ccs.photoframe.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="vertical">
<Button
android:id="#+id/btn_select"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Select Photo"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:background="#drawable/photo_frame"
android:orientation="vertical"
>
<ImageView
android:id="#+id/img_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="70dp"
>
<ImageView
android:id="#+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Save Photo"/>
</LinearLayout>
I am trying to take screen shoot in the LinearLayout "layout_frame"
I tried ...
btn_save_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View v1 = layout_frame.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
}
});
But this taking screen shoot for the entire layout.
Is there any possible way to take screen shoot for the specific location.
Thanks in advance:)
Yes you can get screen shot of any view like this.
ImageView redoo_btn = (ImageView) findViewById(R.id.redoo_btn);
FrameLayout paintLayout = (FrameLayout) findViewById(R.id.drawing);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.drawing1);
Bitmap bm = Takeshot(linearLayout);
Bitmap bm = Takeshot(redoo_btn);
Bitmap bm = Takeshot(paintLayout);
Bitmap Takeshot(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
// view.setImageBitmap(b);
return b;
}
Yes you can do this way:
LinearLayout mlinearlaoytfrme=(LinearLayout)findViewById(R.id.layout_frame);
Bitmap mBItmap=takeScreenshotReceipt();
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
private Bitmap takeScreenshotReceipt()
{
View v1 = getRootView().findViewById(R.id.layout_frame);
v1.setDrawingCacheEnabled(true);
return v1.getDrawingCache();
}
Try this method
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
First I would like to say please do not mark this question a duplicate. It differs from other posts in one critical way. Does anybody know a way that I may divide the color of fragment into two colors right down the middle? Thanks everyone!
here is sample to use gradient
float in[] = new float[] {0f,1f};
TextView textView= (TextView)findViewById(R.id.tv_test);
Shader shader = new LinearGradient(
0, textView.getTextSize(), textView.getWidth(), textView.getHeight(), new int[]{Color.RED, Color.BLUE},in,
Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);
To achieve Second Simply user Framelayout xml
<FrameLayout
android:id="#+id/fl_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll_test"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#android:color/white"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#android:color/holo_red_dark" />
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark" />
</LinearLayout>
<TextView
android:id="#+id/tv_test2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:gravity="center"
android:text="A"
android:textColor="#android:color/white"
android:textSize="100sp" />
</FrameLayout>
For Third one you have to write little and do some work on pixel level
<TextView
android:id="#+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H"
android:textColor="#android:color/holo_red_dark"
android:textSize="100sp" />
<FrameLayout
android:id="#+id/fl_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll_test"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#android:color/white"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#android:color/holo_red_dark" />
<View
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark" />
</LinearLayout>
<TextView
android:id="#+id/tv_test2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:gravity="center"
android:text="A"
android:textColor="#android:color/white"
android:textSize="100sp" />
</FrameLayout>
<ImageView
android:id="#+id/iv_test"
android:layout_width="200dp"
android:layout_height="200dp" />
public class MainActivity extends AppCompatActivity {
View linearLayout;
View tvTest2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView) findViewById(R.id.tv_test);
Shader shader = new LinearGradient(
0, textView.getTextSize()/2, textView.getTextSize(), textView.getTextSize()/2,Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
textView.getPaint().setShader(shader);
linearLayout = findViewById(R.id.ll_test);
tvTest2 = findViewById(R.id.tv_test2);
}
#Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ImageView imageView = (ImageView) findViewById(R.id.iv_test);
Bitmap b1 = getBitmapFromView(linearLayout);
Bitmap b2 = getBitmapFromView(tvTest2);
imageView.setImageBitmap(textEffect(b1, b2));
}
},2000);
}
public Bitmap textEffect(Bitmap image, Bitmap text) {
if (image.getWidth() != text.getWidth() ||
image.getHeight() != text.getHeight()) {
throw new IllegalArgumentException("Dimensions are not the same!");
}
for (int y = 0; y < image.getHeight(); ++y) {
for (int x = 0; x < image.getWidth(); ++x) {
int textPixel = text.getPixel(x, y);
int imagePixl = image.getPixel(x,y);
int red = Color.red(textPixel);
int blue = Color.blue(textPixel);
int green = Color.green(textPixel);
int alpha = Color.alpha(textPixel);
Log.i("TAG", "textEffect: "+x+"-"+y+",-->"+red+","+blue+","+green+","+alpha);
/*Since text color is white so give the color of background to it
* else make it white*/
if (red == 255) {
if (blue == 255) {
if (green == 255) {
image.setPixel(x, y, imagePixl);
}else
{
image.setPixel(x, y, textPixel);
}
}else{
image.setPixel(x, y, textPixel);
}
}else
{
image.setPixel(x, y, textPixel);
}
}
}
return image;
}
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
}
What you exactly want to achieve ??
First one
Second one
Third one ??
Define a TextView
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="O"
android:textColor="#88022b54"
android:textSize="250sp"
android:textStyle="bold"
android:shadowColor="#ff2e7cca"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1"/>
Then
Shader shader = new LinearGradient(
0, mytv.getTextSize()/2, mytv.getTextSize(), mytv.getTextSize()/2,
Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
mytv.getPaint().setShader(shader);
You can do like this
String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));
I achieved this by creating the ideal background in photoshop, saving it as a .png, and then setting that as the activity background for when I need it to be. Simple, clean, easy, and because I only need it to be two colors all the time, I don't have to worry about the image becoming pixelated.
Simple layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.testzoom.app.FullscreenActivity">
<TextView android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="#string/dummy_content" />
<Button android:id="#+id/dummy_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dummy_button"
android:layout_gravity="bottom"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:id="#+id/dummy_image"
/>
</FrameLayout>
Goal - by pressing button on the bottom, set imageview(which is same size as screen) with screenshot bitmap of half size. Result - weird rendering, why fitXY not working?
Click handler:
findViewById(R.id.dummy_button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View view = getWindow().getDecorView();
Bitmap bmp = Bitmap.createBitmap(view.getWidth()/2, view.getHeight()/2, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
c.scale(0.5f,0.5f, bmp.getWidth(), bmp.getHeight());
view.draw(c);
((ImageView)findViewById(R.id.dummy_image)).setImageBitmap(bmp);
}
});
Result on screenshots:
Note that this task is synthetic so don't ask why I am doing this, this is just demonstration of problem.
The problem lies within the call where you are scaling the canvas. You provide the pivot points which are bmp.getWidth() and bmp.getHeight() which are essentially in the middle of the screen.
findViewById(R.id.dummy_button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View view = getWindow().getDecorView();
Bitmap bmp = Bitmap.createBitmap(view.getWidth()/2, view.getHeight()/2, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
c.scale(0.5f,0.5f);//no pivot points
view.draw(c);
((ImageView)findViewById(R.id.dummy_image)).setImageBitmap(bmp);
}
});
I don't think it's the rendering so much as the placement of the new ImageView. You have all three items in a FrameLayout which does nothing to determine the order or placement of view. Where is the code to place the newly created ImageView?